Laravel download files via FTP

This post is going to be about downloading files from the remote server via FTP mechanism in your Laravel application.

Let’s create a controller called DownloadController.php

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

         $ftp = Storage::createFtpDriver([
                        'host'     => 'your_host',
                        'username' => 'your_ftp_user',
                        'password' => 'your_ftp_user_password',
                        'port'     => '21', // your ftp port
                        'timeout'  => '30', // timeout setting 
          ]); 

           $filecontent = $ftp->get($file); // read file content 
           // download file.
           return Response::make($filecontent, '200', array(
                'Content-Type' => 'application/octet-stream',
                'Content-Disposition' => 'attachment; filename="'.$fileName.'"'
            ));
    }
}

Now let’s inform about this controller to routes/web.php

   Route::get('downloadFile','[email protected]');    

That’s it. Now point your browser to http://yourhost.com/downloadFile?file=remote_path/fileName.png.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status