3

I have a script that collects data via ftp and works great. The issue is you have to specify the filename that you want to download.

Example: https://mydomain/downloadFile?file=folder/file1.csv

What i want to do is save all the files in the folder without specifying a filename.

Function:

function downloadFile(Request $request) {

    $file = $request->get('file');
    if(!$file) {
        return Response::json('please provide valid path', 400);
    }
    $fileName = basename($file);

    $ftp = Storage::createFtpDriver([
        'host'     => 'ftp.site.com',
        'username' => 'email.com',
        'password' => 'password',
        'port'     => '21', // your ftp port
        'timeout'  => '30', // timeout setting
    ]);

    $filecontent = $ftp->get($file); // read file content

    // save file.

    Storage::put('file1.csv', $filecontent);

    // download file.

    return Response::make($filecontent, '200', array(
        'Content-Type' => 'application/octet-stream',
        'Content-Disposition' => 'attachment; filename="'.$fileName.'"'
    ));
}
MatHatrik
  • 762
  • 1
  • 6
  • 16

0 Answers0