23

I have 2 disks in Laravel.

One is the local disk, the other one is a FTP server which I need to upload my files to. They are both correctly configured.

I have attempted it this way:

 Storage::disk('FTP')->copy('old/file1.jpg', 'new/file1.jpg');

This would only copy the file if it is already in the FTP server. I have also read the documentation and there seems to be no way to combine both in order to upload files.

Any suggestions?

prgrm
  • 3,734
  • 14
  • 40
  • 80

2 Answers2

70

@ceejayoz has a good answer, but as mentioned in the comments, this fetches, and then writes.

In order to use streams, the following can be used instead:

Storage::disk('FTP')->writeStream('new/file1.jpg', Storage::readStream('old/file1.jpg'));
Dan Jones
  • 1,337
  • 11
  • 22
  • 4
    Just a note that if there is already a file at that location it throws a FileExistsException – nickc Dec 17 '20 at 01:48
41

A simple combination of Storage::get and Storage::put should do the trick.

Storage::disk('FTP')->put('new/file1.jpg', Storage::get('old/file1.jpg'));
ceejayoz
  • 176,543
  • 40
  • 303
  • 368