1

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?

Fractaliste
  • 5,777
  • 11
  • 42
  • 86

1 Answers1

1

Your problem consists of two parts:

  1. Reading FTP file as a stream (see an example with fread(): "PHP: How do I read a .txt file from FTP server into a variable?")

  2. Streaming a Response in Symfony2

Community
  • 1
  • 1
pumbo
  • 3,646
  • 2
  • 25
  • 27