problem uploading a file to an FTP Server

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

Post Reply
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

problem uploading a file to an FTP Server

Post by paulmac1966 »

i am trying to write a php to upload a file to my FTP server (linux / apache)

this is the code to select the file to upload

Code: Select all

<form enctype="multipart/form-data" action="upload_file.php" method="post">
<!-- Choose File to Upload -->
<p>Select the File to Upload</p>
<p><input type="file" name="file_to_upload"></p>

<!-- Choose Which Dir to upload to -->
<p>Which Directory Do You Want to Upload to </p>
<select name="upload_dir">
<option>FC/
<option>MICRO/
<option>PP/
<option>SP/
<option>Tools/
<option>uploads/
</select>

&nbsp

<input type="submit" value="Submit"/>
this then calls the upload_file.php page ->

Code: Select all

<?
$upload_file=basename($_FILES['file_to_upload']['name']);
$upload_dir=$_POST['upload_dir'];
var_dump($_FILES);
 
print "<p>Now Attempting to upload file $upload_file to Dir $upload_dir";
phpinfo();

$target_path=$upload_dir . basename($_FILES['file_to_upload']['name']);
print "<br>Target path is $target_path";#debug

if(move_uploaded_file($_FILES['file_to_upload']['tmp_name'],$target_path))
	{ echo "The file $upload_file has been uploaded to $upload_dir";}
else
	{ echo "There was an error uploading the file, please try again";}
?>
i keep getting the same error

Code: Select all

[client 193.173.33.133] PHP Warning:  File upload error - unable to create a temporary file in Unknown on line 0, referer: http://casi-rusco.serveftp.net/upload.php
[client 193.173.33.133] PHP Notice:  Undefined index:  file_to_upload in /var/www/html/upload_file.php on line 8, referer: http://casi-rusco.serveftp.net/upload.php
[client 193.173.33.133] PHP Notice:  Undefined index:  file_to_upload in /var/www/html/upload_file.php on line 15, referer: http://casi-rusco.serveftp.net/upload.php
[client 193.173.33.133] PHP Notice:  Undefined index:  file_to_upload in /var/www/html/upload_file.php on line 18, referer: http://casi-rusco.serveftp.net/upload.php
[Thu Feb 23 12:18:05 2006] [error] [client 82.71.105.1] File does not exist: /var/www/html/favicon.ico
i have tried to debug this using the var_dump on the $_FILES array and i get the following output

Code: Select all

array(0) { } 
Now Attempting to upload file to Dir FC/ 
Target path is FC/There was an error uploading the file, please try again
it seems like the "file_to_upload" variable is not being posted.

I have even tried to debug this using a windows based php editor with an IDE (DEV-PHP), when i run it it works as i would expect and i get the
following.

Code: Select all

array(1) { ["file_to_upload"]=> array(5) { ["name"]=> string(7) "ftp.cgi" ["type"]=> string(9) "text/html" ["tmp_name"]=> string(38) "C:\Program Files\xampp\tmp\php48D2.tmp" ["error"]=> int(0) ["size"]=> int(1379) } } 
........
The DEV_PHP shows me that the variables are being posted ok, but when i run it on the actual web server, it does not work as shown above.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Just noticing the little things...

1) you NEED to have a MAX_FILE_SIZE before your input type=file !!!!
2) do you end your html form? try print_r($_POST); and print_r($_FILES); and see whats happening
3) why are you doing phpinfo(); right in the middle of your script? that just does not make sence.
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

Post by paulmac1966 »

i put the phpinfo in as it shows all the variables for the page ie

Code: Select all

PHP Variables
Variable Value 
_REQUEST["upload_dir"] FC/ 
_POST["upload_dir"] FC/ 
_SERVER["UNIQUE_ID"] h8MWLcCoAQMAAHv-53QAAAAA 
_SERVER["HTTP_ACCEPT"] image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */* 
_SERVER["HTTP_REFERER"] http://casi-rusco.serveftp.net/upload.php 
_SERVER["HTTP_ACCEPT_LANGUAGE"] en-us 
_SERVER["CONTENT_TYPE"] multipart/form-data; boundary=---------------------------7d657c3da026c 
_SERVER["HTTP_USER_AGENT"] Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; GE Infrastructure IE 6; GE Infrastructure Internet Explorer 6; .NET CLR 1.1.4322) 
_SERVER["CONTENT 
.............etc
i will try your suggestions, thanks
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

Post by paulmac1966 »

I tried your suggestions - output below

Code: Select all

POSTArray ( [MAX_FILE_SIZE] => 30000 [upload_dir] => Tools/ ) 
--------------------------------------------------------------------------------

FILESArray ( [file_upload] => Array ( [name] => rhost.cgi [type] => text/html [tmp_name] => C:\Program Files\xampp\tmp\phpBDFC.tmp [error] => 0 [size] => 380 ) )
It does not seem to like posting the file_upload variable ??????????

Code: Select all

<p><input type="file" name="file_upload"></p>
i cant see anything wrong with this code can you ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The file field is not given in the $_POST variable, but in the $_FILES variable as you may see by your output.
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

Post by paulmac1966 »

if i understand correctly you are saying that the php code

Code: Select all

$upload_dir=$_POST['upload_dir'];
$my_upload=$_POST['file_upload'];
will not allow me to get the "contents" of the file_upload using the $_POST method.
Instead i have to use the $_FILES array
paulmac1966
Forum Newbie
Posts: 16
Joined: Mon Feb 13, 2006 9:37 am

Nearly cracked it

Post by paulmac1966 »

I have realised that the problem was the live server's tmp dir did not have its permissions set correctly
so it would not allow the creation of the tmp file.

my problem was that the $_FILES array had no values assigned to it.

once i changes the tmp dir to a writable dir, now i am getting values for the $_FILES array

it seems like if the files command has a problem it just skips past the line and continues with
the rest of the code (but does not assign anything to the $_FILES).

Now it is creating the tmp file but wont move the tmp file to the designated dir
which again is a Unix permissions problem which i reckon i can sort out.

I should not have ignored the 1st line of the error log which told me it could not write to the tmp dir,
i did not realise what an impact it would have, there a lesson to be learned here

Fix all the simple non relevant (i thought it was not relevant) problems first, they mave have an impact
on the rest of the code.

Thanks for your help all who replied.
Post Reply