Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

ftp

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?

like image 316
Drew McGhie Avatar asked Apr 20 '10 15:04

Drew McGhie


3 Answers

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

like image 55
matei Avatar answered Sep 21 '22 16:09

matei


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.');
like image 24
Bogdanio Avatar answered Sep 22 '22 16:09

Bogdanio


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.

like image 28
Martin Prikryl Avatar answered Sep 21 '22 16:09

Martin Prikryl