Page 1 of 2

Ftp upload problem

Posted: Thu Jan 17, 2008 6:56 am
by Sindarin
I am trying to upload a file to my server via PHP & FTP.
Most of the data are user defined, I successfully connect to my server but I can't seem to get the file to upload,
the $ftp_source_file variable echoes an empty string...

What I want to achieve is to be able to upload a file via ftp in the $ftp_upload_directory, and I need the file ($ftp_destination_file) to be with its original filename. (like, if testimage.jpg was uploaded, then it remains testimage.jpg on the server as well.)

HTML submission form:
<html>
<body>

<form action="getfile.php" method="post" enctype="multipart/form-data">
Server:
<input name="ftp_server" type="text" id="ftp_server" />
username:
<input name="ftp_username" type="text" id="ftp_username" />
Password: <input name="ftp_password" type="password" id="ftp_password" />
<br>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Get this file: <input name="ftp_source_file" type="file" id="ftp_source_file" />
<!--/upload-->
<input type="submit" value="UPLOAD" />
</form>

</body>
</html>
PHP upload script "getfile.php"
<?php

$ftp_server="$_POST[ftp_server]";
$ftp_username="$_POST[ftp_username]";
$ftp_password="$_POST[ftp_password]";
$ftp_upload_directory="/home/base/public_html/test/ftp/";
$ftp_source_file="$_POST[ftp_source_file]";
$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 connection
if ((!$ftp_connection_id) || (!$login_result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftpserver for user $ftp_username";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_username : trying to upload $ftp_source_file<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 "FTP upload has failed to: $ftp_upload_directory . $ftp_destination_file";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// close the FTP stream
ftp_close($ftp_connection_id);
?>

Re: Ftp upload problem

Posted: Thu Jan 17, 2008 6:59 am
by VladSun
I think you should first look at http://php.net/features.file-upload

Re: Ftp upload problem

Posted: Thu Jan 17, 2008 7:09 am
by Sindarin
so
$_FILES['userfile']['name']
actually strips the filename,
now the variable works correctly, but it is having issues to upload to the particular directory. It doesn't pass the upload status check...

Re: Ftp upload problem

Posted: Thu Jan 17, 2008 7:18 am
by VladSun
Read further ;)
You need to use $_FILES['userfile']['tmp_name']

Re: Ftp upload problem

Posted: Thu Jan 17, 2008 7:29 am
by Sindarin
$_FILES['userfile']['tmp_name']

The temporary filename of the file in which the uploaded file was stored on the server.
Can't get it. :(

Re: Ftp upload problem

Posted: Thu Jan 17, 2008 7:35 am
by VladSun
You need to have successful HTTP upload in order to have a successive FTP upload.
The $_FILES['userfile']['tmp_name'] gives you the name of the "HTTP-uploaded file".

Re: Ftp upload problem

Posted: Thu Jan 17, 2008 7:46 am
by Sindarin
You need to have successful HTTP upload in order to have a successive FTP upload.
8O wait. This means I have to use move_uploaded_file first?

I had made a file upload via move_uploaded_file before , but my network admin told me that it would be better and faster if I used ftp functions instead of http move_uploaded_file functions.

Re: Ftp upload problem

Posted: Thu Jan 17, 2008 7:50 am
by VladSun
Now you wait :)

Are the WWW and the FTP server on a same machine? If so, use move_uploaded_file ...

If not - the HTTP uploaded file is located in $_FILES['userfile']['tmp_name'] - you only need to rename it to its original name and to ftp_put() it.

Re: Ftp upload problem

Posted: Thu Jan 17, 2008 8:09 am
by Sindarin
Are the WWW and the FTP server on a same machine?
Not sure what you mean by that... I have ftp access and the public_html files are on the same server.
If not - the HTTP uploaded file is located in $_FILES['userfile']['tmp_name'] - you only need to rename it to its original name and to ftp_put() it.
:( but.. I want to use ftp only *sniff* Not also to mention that I want to upload large files in the future, which through our http upload rate they will timeout.

EDIT: I think something wrong with the path code is going on, I tried adding ftp_chdir and I got
Warning: ftp_chdir() [function.ftp-chdir]: Can't change directory to /home/base/public_html/test/ftp/: No such file or directory in /home/base/public_html/test/ftp/getfile.php on line 27
FTP chdir has failed FTP upload has failed for: /home/base/public_html/test/ftp/urllist.txt
the permissions are 755 and the folder exists.

the code I used is:

Code: Select all

   
 
//change current directory 
    
$ftp_do_chdir=ftp_chdir($ftp_connection_id,$ftp_upload_directory);
    // check ftp change dir
if (!$ftp_do_chdir) { 
        echo "FTP chdir has failed";
    } else {
        echo "FTP chdir has succeeded! Successfully changed to $ftp_upload_directory";
    }
 
right before the upload code snip.

Re: Ftp upload problem

Posted: Mon Jan 21, 2008 4:57 am
by Sindarin
I haven't figured it out yet, any more directions?

Re: Ftp upload problem

Posted: Mon Jan 21, 2008 5:18 am
by VladSun
Your FTP server is chrooted - i.e. you can not have access to parent directories of your user's one. Login in your FTP account by using a simple FTP client, type:

Code: Select all

> pwd
> ls
and see what I mean.

Re: Ftp upload problem

Posted: Mon Jan 21, 2008 7:08 am
by Sindarin
it outputs:

Code: Select all

COMMAND:> [21/1/2008 3:07:01 ??] pwd
           [21/1/2008 3:07:01 ??] 257 "/" is your current location
COMMAND:> [21/1/2008 3:07:06 ??] ls
           [21/1/2008 3:07:06 ??] 500 Unknown command
ERROR:>      [21/1/2008 3:07:06 ??] Syntax error: command unrecognized.
Your FTP server is chrooted - i.e. you can not have access to parent directories of your user's one.
Does this mean I can't upload with ftp from php?

Re: Ftp upload problem

Posted: Mon Jan 21, 2008 7:11 am
by VladSun
Sindarin wrote:Does this mean I can't upload with ftp from php?
No, I mean that you shouldn't give the full system path, but the "FTP path" (URL). It's like you don't write in the address bar "/var/www/public/myname/index.html", but the URL.

Re: Ftp upload problem

Posted: Mon Jan 21, 2008 7:17 am
by Sindarin
So instead of
/home/base/public_html/test/ftp/
to write
/public_html/test/ftp/
as I see in my ftp address bar?

Re: Ftp upload problem

Posted: Mon Jan 21, 2008 7:21 am
by VladSun
Yes - the PHP FTP functions that you use are nothing more than another FTP client ;)