Created
April 26, 2013 10:23
-
-
Save magickatt/5466324 to your computer and use it in GitHub Desktop.
Write data directly to FTP server without saving on the local filesystem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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