Creating and uploading a file in PHP to an FTP server without saving locally

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


Here is matei's solution from above as complete function ftp_file_put_contents():

function ftp_file_put_contents($remote_file, $file_string) {

// FTP login details
$ftp_server='my-ftp-server.de'; 
$ftp_user_name='my-username'; 
$ftp_user_pass='my-password';

// Create temporary file
$local_file=fopen('php://temp', 'r+');
fwrite($local_file, $file_string);
rewind($local_file);       

// FTP connection
$ftp_conn=ftp_connect($ftp_server); 

// FTP login
@$login_result=ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass); 

// FTP upload
if($login_result) $upload_result=ftp_fput($ftp_conn, $remote_file, $local_file, FTP_ASCII);

// Error handling
if(!$login_result or !$upload_result)
{
    echo('<p>FTP error: The file could not be written to the FTP server.</p>');
}

// Close FTP connection
ftp_close($ftp_conn);

// Close file handle
fclose($local_file); }


// Function call
ftp_file_put_contents('my-file.txt', 'This text will be written to your text file via FTP.');

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.


The easiest solution is using file_put_contents with FTP URL wrapper:

file_put_contents('ftp://username:pa‌​ssword@hostname/path/to/file', $out);

If it does not work, it's probably because you do not have URL wrappers enabled in PHP.


If you need greater control over the writing (transfer mode, passive mode, offset, reading limit, etc), use the ftp_fput with a handle to the php://temp (or the php://memory) stream:

$conn_id = ftp_connect('hostname');

ftp_login($conn_id, 'username', 'password');
ftp_pasv($conn_id, true);

$h = fopen('php://temp', 'r+');
fwrite($h, $out);
rewind($h);

ftp_fput($conn_id, '/path/to/file', $h, FTP_BINARY, 0);

fclose($h);
ftp_close($conn_id);

(add error handling)


Or you can open/create the file directly on the FTP server. That's particularly useful, if the file is large, as you won't have keep whole contents in memory.

See Generate CSV file on an external FTP server in PHP.

Tags:

Php

Ftp