Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ftp_put problems

Status
Not open for further replies.

adam560

Programmer
Sep 10, 2006
14
0
0
AU
I'm having trouble with ftp_put. My code is below (with the password changed obviously.

It all works, it allows me to change directories, it will list files in each directory but it wont let me upload files. Other functions work but not ftp_put. I can upload them manually fine so the user does have permission, but can't get it to work with a php script.

I have tried both FTP_ASCII and FTP_BINARY, no luck with either. Any help would be appreciated.

Adam

<?
$host="cpsa.org.au";
$user="cpsa";
$pass="password";
$target="cpsa.org.au/root";
$source = "C:\Documents and Settings\adamb\My Documents\My Pictures\2006-10-25\Image0001.txt";
$fileName = "Image0001.txt";

$conn = ftp_connect($host) or die ("Cannot initiate connection to host");
ftp_login($conn, $user, $pass) or die("Cannot login");
echo ftp_pwd($conn) . "<br />";
echo ftp_chdir($conn,$target);
echo ftp_pwd($conn) . "<br />";
echo ftp_put($conn,$filename,$source,FTP_ASCII);

ftp_close($conn);
?>
 
Two potential problems:

$fileName and $filename are different variables.
$fileName may also need the path. As in, $fileName = "home/public/user_uploads/Image0001.txt";
 
I checked the PHP Manual for this topic, and it seems there are conflicting user comments, and the documentation doesn't explicitly define which goes where, but try swapping around your $fileName and $source variables in the ftp_put() function.
 
That didn't work either. I've modified it so it attempts to put the file in both ways but still no luck. I also simplified the path and file name so that there were less places for it to go wrong. I also tried this code using FTP_BINARY and running it without the IF statements.

Adam

<?
$host="cpsa.org.au";
$user="cpsa";
$pass="password";
$target="cpsa.org.au/root";
$source = "C:\a.txt";
$filename = "a.txt";

$conn = ftp_connect($host) or die ("Cannot initiate connection to host");
ftp_login($conn, $user, $pass) or die("Cannot login");

echo ftp_chdir($conn,$target);

if (ftp_put($conn,$source,$filename,FTP_ASCII)){
echo("success 1");
}
if (ftp_put($conn,$filename,$source,FTP_ASCII)){
echo("success 2");
}
ftp_close($conn)
?>
 
I think I might know the problem. I load this script onto my webserver and then run it by navigating to it in IE. Instead of finding c:\a.txt on my computer it may be looking for the same file on my servers computer/

If this could be the problem does anyone know how to fix it?
 
You would have to have the file submitted via form data, I do believe.

Give that a try if you haven't already. My apologies on the slow reply, my wife went into labor while I was responding to many of these topics :)
 
If you hahevn't tried it,
JBlair is correct, you best use a form with a file input and take that value for upload. Make sure the file really exists as well.

When I changed to using the input type file it suddenly all worked.

What you could also do is check what the contents of the form input field looks like to php... I think you'll get something like C:\\file.txt

JR (IT = Logic (except for a well known OS where it equals luck) -> Back to the Basics!
 
Hey, thanks. I've gotten it working now, you were both right. Congrats JBlair, and the slow response was no problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top