Link to home
Start Free TrialLog in
Avatar of amoenum123
amoenum123

asked on

problem with php ftp_put command

I'm having a really difficult time trying to resolve this problem...here's the situation:

We have a personals website on one server and want to members to be able to upload and store videos of themselves.  We want the videos to be stored on a different server.

I'm using the ftp functions in php to try and accomplish this.  I seem to have no problems connecting to the server or logging in, but when it comes to actually transferring the files, the script is failing.

Here's the code:

$file = $userfile[$i];//temp file name
$remote_file = $vidname;
$tmpName = basename($userfile[$i]);
move_uploaded_file($file, $vid_working_dir."/".$tmpName) or die("Cannot move uploaded file to working directory");

// set up basic connection
$conn_id = ftp_connect($vid_ip_address,21) or die("Cannot initiate connection to host");

// login with username and password
$login_result = ftp_login($conn_id, $vid_username, $vid_password) or die("could not login");

// upload a file
$savepath = $vid_root_dir.$vid_file_path.$vidname;
$upload = ftp_put($conn_id, $savepath, $vid_working_dir."/".$tmpName, FTP_BINARY);
if (!$upload) {

    echo "Cannot upload";
      

} else {

    echo "Upload complete";
      
}


// close the connection
ftp_close($conn_id);
unlink($vid_working_dir."/".$tmpName) or die("Cannot delete uploaded file from working directory -- manual deletion recommended");


My error logs are giving me the following:

ftp_put(): /var/www/vhosts/vid.xxxxxx.net/httpdocs/videos/1_p61824.mpg: No such file or directory in /var/www/vhosts/xxxxxx.net/httpdocs/php/uploadvideos.php on line 454, referer: http://xxxxxx.net/index.php?&action=uploadvideos

The video is being uploaded to the main server in the temp location without any problem.

What it looks like to me is that it's trying to find the file before it's actually uploaded....this is the first time I've tried to use the ftp commands so I have no experience with this command.

Your help is much appreciated...I'm on a deadline to get this site up and running!

Regards,
Avatar of arnold
arnold
Flag of United States of America image

It is not clear how you are handling the transaction of getting the file uploaded to the web server and then the transfer out, or are you trying to skip the intermediary step of storing the file localy by redirecting the stream through an FTP session.

Your best bet would be to get the file first and then have a cron or a scheduled process that would transfer the file. (Presumably you use a database.)
Avatar of amoenum123
amoenum123

ASKER

sorry for not being clear on that part....

I'm not having any trouble with the file upload (using standard file upload in a form) and i've confirmed that it is storing it locally first.

The problem seems to lie when I attempt to transfer the file between servers.  I have no problems connecting to the other server and can log in with the username and password fine.
The issue is at what stage are you attempting the transfer out (ftp)?  If you are attempting to do so from the script that handles the upload, double check that the ftp session does not begin until the upload is complete.  Or use asynchronous transfer.  I.e. when a file is uploaded mark its detail in a database.  Then have a cron job or a process that checks the database for uploaded files and transfer them out.

 
ok now i've tried running the following as a standalone process (knowing that the source file exists):

// set up basic connection
$conn_id = ftp_connect("vid.xxxxx.com",21) or die("Cannot initiate connection to host");

// login with username and password
$login_result = ftp_login($conn_id, "username", "passwod") or die("could not login");
i
if(file_exists("/var/www/vhosts/xxxxx.com/httpdocs/workdir/1_p61822.mpg"))
echo "file exists";

// upload a file
$upload = ftp_put($conn_id, "/var/www/vhosts/vid.xxxxx.com/httpdocs/videos/thisvideo.mpg", "/var/www/vhosts/xxxxx.com/httpdocs/workdir/1_p61822.mpg", FTP_BINARY);

if (!$upload) {

    echo "Cannot upload";
      

} else {

    echo "Upload complete";
      
}


i get the output "file exists" after the test for the source file and yet I still get "Cannot upload" as the result.  My error logs are still telling me that the file doesn't exist.  I'm pulling my hair out going crazy.

I'm able to connect to the server...
I'm able to log in.....
The source file exists....
The permissions on the destination directory are set properly...
ok i seem to have made it a little bit further....

I've changed the destination path from
/var/www/vhosts/vid.xxxxx.com/httpdocs/videos/thisvideo.mpg

to
httpdocs/videos/thisvideo.mpg

and now it's actually started to create the file at the destination, but it's of zero size and won't go any further....
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well i seem to be beating my head against the wall here....

thanks for your help arnold and your patience...i'm going to have to investigate this further using some other method because it's simply not working for me...I'm obviously missing something.

cheers
sean