[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

problem with php ftp_put command

Posted on 2006-05-29
7
Medium Priority
?
349 Views
Last Modified: 2013-11-13
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,
0
Comment
Question by:amoenum123
  • 4
  • 3
7 Comments
 
LVL 81

Expert Comment

by:arnold
ID: 16786569
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.)
0
 

Author Comment

by:amoenum123
ID: 16786693
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.
0
 
LVL 81

Expert Comment

by:arnold
ID: 16787121
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.

 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:amoenum123
ID: 16793158
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...
0
 

Author Comment

by:amoenum123
ID: 16793383
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....
0
 
LVL 81

Accepted Solution

by:
arnold earned 1500 total points
ID: 16794062
It seems that whether the file is there or not, the upload will be attempted.  You should make sure that the file has been uploaded prior to attempting the ftp.

Are you sure the location for the remote file is writeable?
can you from the system FTP a file to the remote location with the same credentials?

Have you considered whether you need to use a passive connection?

http://us2.php.net/manual/en/function.ftp-put.php

It might be better to use scp/sftp to avoid transmitting username/password information in plain text: http://us2.php.net/manual/en/function.ssh2-sftp.php

however,  you should consider an asynchronous transfer of uploaded files.  Or use a network mapped (NFS) drive that is accessible from both systems.

0
 

Author Comment

by:amoenum123
ID: 16794725
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
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Simple Linear Regression
Loops Section Overview

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question