Skip to content

Instantly share code, notes, and snippets.

@kauhat
Created December 14, 2017 12:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kauhat/e6ebc7c320f60c7b8464606403f7d913 to your computer and use it in GitHub Desktop.
Save kauhat/e6ebc7c320f60c7b8464606403f7d913 to your computer and use it in GitHub Desktop.
Upload & copy files between disks in Laravel 5
<?php
use Illuminate\Http\File;
use Storage;
/**
* Load contents of file into memory and copy. Slower, but can copy from
* remote disks.
*
* @return void
*/
public function copyToDisk($local_disk, $local_path, $remote_disk, $remote_path)
{
// Load local file into memory
$local_contents = Storage::disk($local_disk)->get($local_path);
// Stream file contents
return Storage::disk($remote_disk)->put($remote_path, $local_contents);
}
/**
* Copy using streamable reference to file. Useful for bigger files.
* Requires full file path and will only work with local files.
*
* @return void
*/
public function copyToDiskWithStream($local_disk, $local_path, $remote_disk, $remote_path)
{
// Get full path
$local_fullpath = Storage::disk($local_disk)
->getDriver()
->getAdapter()
->applyPathPrefix($local_path);
// Create a reference to the local file
$file_local = new File($local_fullpath);
// Split up remote file path
$remote_directory = dirname($remote_path);
$remote_name = basename($remote_path);
// Stream from reference
Storage::disk($remote_disk)->putFileAs($remote_directory, $file_local, $remote_name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment