I need to serve to my web portal enduser a file stored on a FTP server.
I create a local temp file, I download the FTP's file in my temporary file, and when it finish I send it with Symfony's file HTTP response:
$tempPath = tempnam(sys_get_temp_dir(), 'ftp');
$tempFile = fopen($tempPath, 'a+');
$log = "myLog";
$pass = "myPwd";
$conn_id = ftp_connect('my.ftp.server');
$ftp_login = ftp_login($conn_id, $log, $pass);
ftp_fget($conn_id, $tempFile, $path, FTP_ASCII, 0);
ftp_close($conn_id);
fclose($tempFile);
return $app->sendFile($tempPath, 200, array('Content-Type' => 'application/force-download', 'Content-disposition' => "attachment; filename=myFile.zip"));
It works but I would do better (ensure temp file deletion, improve performance, ...)
I see that Symfony provides HTTP streamed response helper, so I imagine to send the FTP's file without to store it on web server's hard drive disk.
To get it I think I need to connect ftp_fget()
function (or other FTP function) to a PHP function that can output it to php://output
or standard output (I don't really know this PHP scope).
I find the readfile()
function but it take the filename as argument, not a resource like ftp_fget()
need... Is there other function compatible with fget_fget or other way to do it?