Re: Ftp upload problem
Posted: Mon Jan 21, 2008 7:53 am
Thanks, I'll keep struggling with it for a while. 
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
<?php
// set up ftp connection
$ftp_server="$_POST[ftp_server]";
$ftp_username="$_POST[ftp_username]";
$ftp_password="$_POST[ftp_password]";
$ftp_upload_directory="/public_html/test/ftp/";
$ftp_source_file = $_FILES['ftp_source_file']['name'];
$ftp_destination_file=$ftp_source_file;
$ftp_connection_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($ftp_connection_id, $ftp_username, $ftp_password);
// check ftp connection
if ((!$ftp_connection_id) || (!$login_result)) {
echo "<font color='red'>Error:</font> FTP connection has failed!<br>";
echo "Attempted to connect to $ftpserver for user $ftp_username";
exit;
} else {
echo "<font color='green'>Success:</font> Connected to $ftp_server, for user $ftp_username : trying to upload $ftp_upload_directory$ftp_source_file<br>";
}
//change current working directory
$ftp_do_change_working_directory=ftp_chdir($ftp_connection_id,$ftp_upload_directory);
//check ftp change dir
if (!$ftp_do_change_working_directory) {
echo "<font color='red'>Error:</font> FTP chdir has failed";
} else {
echo "<font color='green'>Success:</font> FTP chdir has succeeded! Successfully changed to $ftp_upload_directory <br>";
}
// upload the file
$ftp_do_upload = ftp_put($ftp_connection_id, $ftp_upload_directory.$ftp_destination_file, $ftp_source_file, FTP_BINARY);
// check upload status
if (!$ftp_do_upload) {
echo "<font color='red'>Error:</font> FTP upload has failed for: $ftp_source_file when trying to store as $ftp_upload_directory$ftp_destination_file<br><br>";
} else {
//successfully uploaded the file
echo "<font color='green'>Success: </font>Uploaded $source_file to $ftp_server as $destination_file <br>";
}
// close the ftp connection
ftp_close($ftp_connection_id);
?>