How to create ZIP download in Lavarel from files stored on remote FTP server?

Viewed 491

I need create ZIP file from folder in other host (subdomain) with FTP.

I need get download link from FTP.

$id = 1;
$zip = new ZipArchive;
$fileName = 'myNewFile.zip';

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

    $path = storage_path('/series/sound/' . $id);
    $files = Storage::disk('ftp')->files($path);

    foreach ($files as $key => $value) {

        $relativeNameInZipFile = basename($value);
        $zip->addFile($value, $relativeNameInZipFile);
    }
    $zip->close();
}
return response()->download(storage_path($fileName));
1 Answers

You have to download the files from FTP to the webserver, to be able to add the files to a ZIP file you are creating on the web server.

I do not know Lavarel, but something like this should do do:

$contents = Storage::disk('ftp')->get($value);
$zip->addFromString($contents, $relativeNameInZipFile);

This downloads the files to webserver memory. That might not work, if the files are too large. In that case you would have to download them to temporary files on the web server instead.

Related