skip to Main Content

I’m asking for your help because I can’t figure out where the error is.
I’m creating a backup zip file of a folder in Laravel and I want to automatically save it on the Amazon S3 server. I’ve already set up the entire FyleSystem, ID keys etc.. the point is that now on the Amazon server it only saves the name of the file and not the archive .zip, can I ask for your help to understand why it doesn’t save the entire archive but only the name?

This is code

  $zip = new ZipArchive;

        $azienda = User::value('azienda');

        $fileName = time() . $azienda . ".zip";

        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE) {

            $files = File::files(public_path('/images/images'));

            foreach ($files as $key => $value) {
                $relativeNameInZipFile = basename($value);
                $zip->addFile($value, $relativeNameInZipFile);

            }
            $zip->close();
        };

        Storage::disk('s3')->put('public/backup', $fileName);

enter image description here

thanks a lot to everyone

2

Answers


  1. Storage::put takes either a string contents or a file resource. It does not take file names, and will treat it as the string it is.

    https://laravel.com/docs/10.x/filesystem#storing-files

    Storage::disk('s3')->put('public/backup', fopen($filename, 'r'));
    

    or, for lower memory usage, stream it:

    Storage::disk('s3')->putFile('public/backup', new IlluminateHttpFile($filename));
    
    Login or Signup to reply.
  2. Storage::disk(‘s3’)->put(‘public/backup’, $fileName);

    You are storing file name instead of content!

    Try this:

    $content = Storage::get($fileName);
    Storage::disk('s3')->put('avatars/1', $content);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search