Skip to content

Instantly share code, notes, and snippets.

@magickatt
Created April 26, 2013 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magickatt/5466324 to your computer and use it in GitHub Desktop.
Save magickatt/5466324 to your computer and use it in GitHub Desktop.
Write data directly to FTP server without saving on the local filesystem
<pre>
<?php
// Host
$host = 'yourhostname.com';
$port = 21;
$timeout = 90;
// Credentials
$username = 'yourusername';
$password = 'yourpassword';
// Data to write and destination
$data = 'Some data to be written to a file';
$path = 'path/on/the/ftp/server';
$filename = 'test.txt';
// Connect to FTP
$connection = "ftp://$username:$password@$host/$path/$filename";
$handle = fopen($connection, "w");
if (! $handle) {
echo 'Could not connect to FTP server';
} else {
echo 'Connected to FTP server';
}
// Write to FTP
$bytes = fwrite($handle, $data);
if (! $bytes) {
echo 'Could not write file to FTP server';
} else {
echo 'Written file to FTP server';
}
// Disconnect from FTP
$closed = fclose($handle);
if ($closed) {
echo 'Closed connection to FTP server';
} else {
echo 'Could not close connection to FTP server';
}
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment