Creating and uploading a file in PHP to an FTP ser

2020-02-04 06:31发布

I'm having a problem connecting two 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?

标签: php ftp
4条回答
别忘想泡老子
2楼-- · 2020-02-04 06:57

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.

查看更多
再贱就再见
3楼-- · 2020-02-04 06:59

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.');
查看更多
孤傲高冷的网名
4楼-- · 2020-02-04 07:06

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

查看更多
劫难
5楼-- · 2020-02-04 07:16

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.

查看更多
登录 后发表回答