Problm uploading files (Trough ftp)

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
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Problm uploading files (Trough ftp)

Post by Luis Almeida »

I have the folowing problem, I use this code to upload files to my server (Sambar):

<?php
if(!empty($_POST['uploadfile']))
{
$filepathname = $_POST['uploadfile'];
$filename = basename($filepathname);
$uid = '******';
$pwd = '*******';
$host = '*********';
$ftp = ftp_connect($host) or die("Couldn't connect to $host");
ftp_login($ftp,$uid,$pwd) or die("Couldn't log in to $host");
ftp_chdir($ftp,"/");
if(ftp_put($ftp,$filename,$filepathname,FTP_BINARY))
{
ftp_close($ftp);
print("<META HTTP-EQUIV='Refresh' CONTENT='1;url=$_SERVER['PHP_SELF']'>");
}
else
{
die("<br>Could not transfer $filename to $host<br>");
}

}
else
{
echo "teste1";

The problem is this code works well on my PC (The one with the server) but when I try using this code on the intranet or over the internet it doesn't work, could someone help me please?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What do you mean by 'it doesn't work'?

Mac
rashed
Forum Newbie
Posts: 17
Joined: Fri Jun 06, 2003 6:27 am
Location: Islamabad, Pakistan.

Post by rashed »

If you please write the error/warning message
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Post by Luis Almeida »

With "It doesn't work" I mean that "It doesn't work"



this lines of code work just fine:

Code: Select all

ftp = ftp_connect($host) or die("Couldn't connect to $host"); 
   ftp_login($ftp,$uid,$pwd) or die("Couldn't log in to $host"); 
   ftp_chdir($ftp,"/");
but when it comes to the transfer itself

Code: Select all

ftp_put($ftp,$filename,$filepathname,FTP_BINARY);
the ftp_put function returns false and I dont understand why, because like I said the code works well on my PC (The one with the server) but when I try using this code on the intranet or over the internet it doesn't work.
It just doesn´t upload anything (not even one byte)
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

I have a suspicion that you are trying to access a directory that your not supposed to, the ftp_chdir() method returns false if it's unable to change the directory.

I suspect this because you are trying to change to the root ("/", not as in the user root, but probably owned by root!) directory of the HDD you are on, which may be outside of your users agreed access.

Just a thought.

Regards,

To add:

To check if you are in your chosen directory use:

http://uk2.php.net/manual/en/function.ftp-pwd.php
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Post by Luis Almeida »

the ftp_chdir() method works fine because it returns ( 1, true) and the scripts also works fine if I call it from the PC with the server installed on but when it comes to execut it fron another PC on the Intranet or the Internet it Just doesn´t Work.
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Is there anything in the $php_errormsg variable?
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Post by Luis Almeida »

Nop,($php_errormsg ) it´s empty as clean air
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Can you try listing the files in the dir:

Code: Select all

<?php
ftp_nlist($ftp, '/');
?>
What version of PHP are you using?
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post by cactus »

Can you show us the state of your variables, add some debug code to echo out the values of $filepathname and $filename.

Also, enable some of the debug features of PHP (in case they are turned off, esp. in a production env.):

Code: Select all

error_reporting(E_ALL); 
ini_set('display_errors', TRUE);
BTW, the $php_errormsg is only applicable if you have enabled "track_errors", it is disabled by default (0).

Ref:
http://uk.php.net/manual/en/ref.errorfu ... ack-errors
http://uk.php.net/manual/en/language.op ... ontrol.php

Regards,
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Post by Luis Almeida »

Thats correct CACTUS, after adding:

Code: Select all

<?php
error_reporting(E_ALL); 
ini_set('display_errors', TRUE);
?>
I get the following error message:

Code: Select all

Warning: ftp_put(): error opening C:\TEMP\Onlyin.pps in G:\sambar60\docs\upload_last.php on line 22
Now I dont understand WHY, because I´m the administrator of my computer and it still works on the PC with the server installed on it
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Post by Luis Almeida »

Another thing,... thi file "ONLYIN.PPS" is only 563Kb size
My PHP is Version 4.2.3
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Have you checked that the file exists?
User avatar
Luis Almeida
Forum Commoner
Posts: 33
Joined: Tue Apr 01, 2003 4:22 am

Post by Luis Almeida »

Ok, I found something very strange.....
Let me try to explain it
It won't be easy......

the thing is.....

1 - I´m currently working on a IntraNet
2 - There are two PC´s
3 - PC number One is the one with the server installed on it and whre the script works just fine
4 - PC number two is the one from wich I can´t upload anything

I found the following
the error message :

Warning: ftp_put(): error opening C:\TEMP\Onlyin.pps in G:\sambar60\docs\upload_last.php on line 22

is because whe I try to upload a file from PC number TWO it will try to upload the file from PC number ONE wich means tha the reference to the file is to the PC with the SEERVER installed (PC numer ONE)

How can I correct this ???
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

By server, I presume that you're talking about the FTP server!

What machine is the PHP script running on?
Post Reply