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

前端 未结 4 1624
感动是毒
感动是毒 2020-12-08 23:55

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

4条回答
  •  温柔的废话
    2020-12-09 00:40

    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('

    FTP error: The file could not be written to the FTP server.

    '); } // 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.');

提交回复
热议问题