lundi 31 juillet 2017

Laravel 5.4 usage of "Storage::putFile"

I am working with Laravel 5.4 and want to upload a file (let's call it "lorem.ext") to the storage directory (storage/app/) "path/to/file" with a unique file name.

For that I want to use Storage::putFile (http://ift.tt/2tRusQt) which not only stores my file, but also automatically creates a unique file name.

The documentation says to use such:

Storage::putFile('uploadedFile', new File('/path/to/file'));

Using this, I will get the error

FileNotFoundException in File.php line 37: The file "/path/to/file" does not exist

My further thoughts:

I honestly do not know exactly what the signature means and never found a working example from putFile in the web. In the documentation it is not described and looking closer (http://ift.tt/2vfkr3r) there is no information, as well.

What I think it means:

The first parameter "uploadedFile" (or as http://ift.tt/2tRusQt calls it: "photos") will automatically get the file via the ID from the form in the view:

<input type="file" id="uploadedFile" name="uploadedFile">

and there is no need anymore to load it via

request()->file('uploadedFile')

The second parameter "new File('/path/to/file')" (or as http://ift.tt/2tRusQt calls it: "new File('/path/to/photo')") will specify the path in the target storage directory on the server without the file name:

.../storage/app/path/to/file/formerLoremNowUniqueFileName.ext

So complete example where I can upload my lorem.ext and it will get stored on .../storage/app/path/to/file/formerLoremNowUniqueFileName.ext (which does not work):

View:

<form method="POST" action="URL/TO/STORE" enctype="multipart/form-data">
    
    <input type="file" id="uploadedFile" name="uploadedFile">
    <button type="submit" class="btn btn-primary">Upload</button>
</form>

Controller:

public function store() {
    return Storage::putFile('uploadedFile',  new File('/path/to/file'));
}

Can anybody

  1. describe the signature of "putFile" so that I get it? ;-)
  2. tell me why my example is wrong and why I do get this FileNotFoundException?

Thank you!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tR6eGl
via IFTTT

Aucun commentaire:

Enregistrer un commentaire