Sample code showing how to use PHP's FTP functionality to transfer a file from one computer to another.
// Set up FTP variables (sample values are used here)
$host = 'xyz.abcdef.com'; // host where you are going to transfer the file to
$user = 'adcdef.com'; // username to log onto the host
$password = 'shccscb'; // password to log onto the host
$sourcefile = $upfile; // file to be transferred
$targetfile = 'public/'.$upfile; // where it is to be transferred to
// connect to FTP site
$conn = ftp_connect("$host");
if (!$conn)
{
echo 'Error: Could not connect to ftp server<br>';
exit;
}
echo "Connected to $host.<br><br>";
// log in to target site
@ $result = ftp_login($conn, $user, $password);
if (!$result)
{
echo "Error: Could not log on as $user<br>";
ftp_quit($conn);
exit;
}
echo "Logged in as $user<br><br>";
// transfer file to target site
if (ftp_put($conn, $targetfile, $sourcefile, FTP_ASCII)) {
echo "Successfully transferred $sourcefile\n";
}
else {
echo "There was a problem while transferring $sourcefile\n";
ftp_quit($conn);
exit;
}
// close the connection
ftp_close($conn);
Go back to PHP Tutorials home page
Go back to Tutorials home page
|
|
|