Created
March 18, 2015 16:31
-
-
Save marcusmoore/16be532dcb600bfade67 to your computer and use it in GitHub Desktop.
Laravel 5 Filesystem confusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* By default, the storage path for local is set to |storage_path() . '/app'| which means | |
* when you use |Illuminate\Contracts\Filesystem\Factory| contract and do something like | |
* $storage->disk('local')->get('temp/' . $filename) you are effectively working within | |
* |storage/app/temp| not |storage/temp| as you might have expected. Because you might | |
* think to move a file with |$request->file('image)->move(storage_path('temp'), $filename))| | |
* this will fail because storage_path() returns the storage directory (storage/), not | |
* |storage/app| | |
* | |
* Something like the following works but is VERY ugly. | |
*/ | |
// Set the filesystems | |
$localFilesystem = $storage->disk('local'); | |
$s3Filesystem = $storage->disk('s3'); | |
// Grab uploaded file | |
$uploadedFile = $request->file('image'); | |
// Set the filename | |
$filename = strtolower($request->get('first_name') . '_' . $request->get('last_name') . '.' . $uploadedFile->getClientOriginalExtension()); | |
// Move the file to a temp location (app/temp/filename.ext) | |
$uploadedFile->move(storage_path('app/temp'), $filename); | |
// Grab file contents via filesystem | |
$localFile = $localFilesystem->get('temp/' . $filename); | |
// Move to s3... | |
$s3FilePath = 'staff/images/' . $filename; | |
$s3Filesystem->put($s3FilePath, $localFile); | |
// Set to public | |
$s3Filesystem->setVisibility($s3FilePath, 'public'); | |
// Delete temp file | |
$storage->disk('local')->delete('temp/' . $filename); | |
return $filename; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment