Asked  7 Months ago    Answers:  5   Viewed   32 times

The server which I'm hosting my website does not support links so I cannot run the php artisan storage:link to link my storage directory into the public directory. I tried to remake the disk configuration in the filesystems.php to directly reference the public folder but it didn't seems to work either. Is there a way to upload a file using Laravel libraries directly into the public folder or will I have to use a php method? Thanks in advance.

 Answers

52

You can create a new storage disc in config/filesystems.php:

'public_uploads' => [
    'driver' => 'local',
    'root'   => public_path() . '/uploads',
],

And store files like this:

if(!Storage::disk('public_uploads')->put($path, $file_content)) {
    return false;
}
Wednesday, March 31, 2021
 
TheFrack
answered 7 Months ago
17

Try adding processData: false, contentType: false in your code

Replace your script with this:

function submitDocument(){
var formData = new FormData(); // Currently empty
var _token = $("#_token").val().trim();
formData.append('title', $("#title").val());
formData.append("doc",$("#doc")[0].files[0]);
$.ajax({
  url: "documents",
  method: "post",
  data:{_token,formData},
  cache : false,
  processData: false,
  contentType: false
}).done(function(data) {

});
return false;// Not to submit page
}

By default, data passed in to the data option as an object will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

Wednesday, March 31, 2021
 
pwaring
answered 7 Months ago
40

Your server can't locate storage as path because it uses public_path run

php artisan storage:link

It should work. Also, try to use

{{ asset('storage/'.$ad->avatar) }}
Wednesday, March 31, 2021
 
shin
answered 7 Months ago
100

Reading the comments I think you want to do the following:

  • Editing anothers profile (or anything else)
  • your rights have to be higher than the ones of the other account
  • everything should be logged by the user that changed the entries, not by the owner

The following solutions are build in ones, maybe there are some packages for laravel to solve this kind of problem.

Auth::loginById($otherUserId) could be one solution:

  • you have to check if the user is allowed to log in in this profile
  • you have to remember your own user id (in a session) to add it for the log
  • you can access only the pages the user can see (not the admin pages)

Another approach would be to use Policies

e.g. you are user 1 and want to edit the profile of user 3. in the update function user/3/profile. You call a policy function where you check if your user_role_id is smaller than the other ones. Then the record will be saved and the logger will log it away with your user id.

Both ways have pros and cons. Login with the id will give you exact the view of the other user. But you have to modify your logger (instead of Auth::id() use something with a session). Then you can implement a little button with (jump back to own profile) to login back in your own account. Using polices will be easier for the logger, but at every part you have to implement the check with the policy.

Not knowing the size and complexity of your project I would suggest the first solution. I implemented it by myself in one project but without the logger function.

Saturday, August 14, 2021
 
Jared
answered 2 Months ago
36

If your goal is to upload a file to a directory you shouldn't have to use Carrierwave or Paperclip. Those gems have a lot of support for image processing and extra options.

I suggest you look at the Ruby file class and the open method to be more specific. http://www.ruby-doc.org/core-1.9.3/File.html#method-c-open

Something like the following should do the trick:

# "public/csv" is the directory you want to save the files in
# upload["datafile"] is the data populated by the file input tag in your html form 
path = File.join("public/csv", upload["datafile"].original_filename)
File.open(path, "wb") { |f| f.write(upload["datafile"].read) }

Keep in mind, your public directory is accessible to the world. If you need to save these in a more private location, make sure the directory is only readable and writable by your app.

Also, if you are working with CSV files, be sure to read through the Ruby CSV class: http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html. It makes working with CSV files a breeze.

Sunday, August 15, 2021
 
altermativ
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :