Ftp upload problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Ftp upload problem

Post by Sindarin »

Thanks, I'll keep struggling with it for a while. :D
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Ftp upload problem

Post by Sindarin »

My current code again it is:

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); 
?>
Tried chdir this time, it worked, so it seems the cwd is indeed set to /public_html/test/ftp/, but the file is not uploaded?

I provide additional info with phpinfo:
my ftp access is enabled, I can upload up to 200MB,
upload_tmp_dir and user_dir is no value? Could it be the problem?
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Ftp upload problem

Post by Sindarin »

Anyways I can't seem to find a solution so I'll just use move_uploaded_file and cross my fingers. :?
Thanks guys and gals.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Ftp upload problem

Post by VladSun »

That's the easiest solution - just make sure your Apache user has write access to this folder. :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Ftp upload problem

Post by Sindarin »

heheh, I have used it before successfully, what can go wrong now? :D
I might even consider making a flash applet for this so I can watch the progress.
Post Reply