tags:

views:

38

answers:

2

I'm having a problem connecting 2 different processes that I have working. I've been tasked with pulling data out of a database, creating a file from the data, and then uploading it to an ftp server.

So far, I have the file being created and downloadable using this code, $out being a string that contains the complete text file:

if ($output == 'file')
{
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: ".strlen($out));
    header("Content-type: application/txt");
    header("Content-Disposition: attachment; filename=".$file_name);
    echo($out);
}

This works when I want to just run the script in a browser and download the file, but I'm looking to instead send it to an ftp server.

I know my connection to the ftp server is working just fine, and I'm correctly navigating to the correct directory, and I've taken files from disk and put them on the ftp using ftp_put(), but I'm looking to take $out and write it directly as a file with $filename as its name on the FTP server. I may be misreading things, but when I tried ftp_put and ftp_fput, it seemed that they want file locations, not file streams. Is there a different function I could consider?

A: 

Actually ftp_put expects the path to the local file (as a string), so try to write the data in a temporary file and then ftp_put it to the server


file_put_contents('/tmp/filecontent'.session_id(), $out);
ftp_put($ftp_conn, $remote_file_name, '/tmp/filecontent'.session_id());
unlink('/tmp/filecontent'.session_id()); 

In this case you don't need to send the headers you were sending in your example.

matei
Is there a way to do this without making a file on the file system though?
Drew McGhie
+1  A: 

I've answered here instead of the comments since comments ignore code formatting.

you could try:


$fp = fopen('php://temp', 'r+');
fwrite($fp, $out);
rewind($fp);       
ftp_fput($ftp_conn, $remote_file_name, $fp, FTP_ASCII);

this will create a temporary stream without actually writing it to disk. I don't know any other way

matei
That's what I'm looking for, thanks.
Drew McGhie