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?
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.');
The easiest solution is using file_put_contents
with FTP URL wrapper:
file_put_contents('ftp://username:password@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With