...

View Full Version : PHP FTP Upload Script Help



charbsk
08-10-2007, 12:07 AM
Hey Everyone,

I've been working on a PHP script that allows a user to upload a file from a Form via FTP to my server. The problem I'm having is that the file will not get uploaded to the server. I know that its not a Server issue because a connection is being established and I was able to see a listing in the current directory I am in on the Server. I have CHMOD the permission on the directory to 777, and I've also turned on Passive Mode in my script. But it still comes back saying that the file doesn't exist and that there was a problem uploading the file. Below is my code, any help would be appreciated.

I put the variable $server instead of my actual HOST url, cause I don't want people accessing it, but i will change it back once I can get help on how to get this script working. PHP Code below as well as my FORM. Thanks


<?php

///phpinfo();
// FTP Configuration
$FTP_User = "$username";
$FTP_Pass = "$password";
$FTP_Host = "$server";
$FTP_Root = "httpdocs/charbel/upload/";

// If the form was submitted
if (isset($action) && $action == "submit")
{

// Connect to the ftp address
$Connect = ftp_connect($FTP_Host);

if (!$Connect)
{
echo "Error: Could not connect to ftp server<br>";
exit;
}

echo "Connected to $FTP_Host<br>";

// Login
$login = ftp_login($Connect, $FTP_User, $FTP_Pass);

// Turns passive mode on
$passive = ftp_pasv ($Connect, true );

echo "Current directory is now: " . ftp_pwd($Connect) . "<br>";

if (ftp_chdir($Connect, "$FTP_Root"))
{
echo "Current directory is now: " . ftp_pwd($Connect) . "<br>";
} else echo "Cannot change directory";

$contents = ftp_nlist($Connect, ".");

// Output directory listing
print_r($contents) . "<br>";

// check upload status
if (!passive){
echo "Failed to enter passive mode.<br>";
}
else {
echo "Entered passive mode.<br>";
}

if (!$login)
{
echo "Error: Could not log on as $FTP_User<br>";
ftp_quit($Connect);
exit;
}

echo "Logged in as $FTP_User<br>";

// Set the filename to be uploaded
$Filename = $FTP_Root . $_FILES['File_1']['name'];


if (file_exists($Filename))
{
echo "The file $Filename exists<BR>";
}
else
{
echo "The file $Filename does not exist<BR>";
}

// Set the local resource (the file that will be uploaded)
$Local_Resource = $_FILES['File_1']['tmp_name'];

// If the file was successfully uploaded
$upload = ftp_put($Connect, '$Filename', '$Local_Resource', FTP_BINARY);

if (!$upload)
{
// Show error message
echo "There was a problem uploading $Local_Resource to $Filename";
}
else
{
// Else show success message
echo "Successfully uploaded $Filename";
}
ftp_quit($Connect);
}

?>


Here is my Form Code:


<html>
<head>
<title> PHP FTP Upload Test </title>
</head>
<body>
<table>
<form method="post" action="?action=submit" enctype="multipart/form-data">
<tr>
<td>Username:</td>
<td><input type="text" name="username" size="30"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" size="30"></td>
</tr>
<tr>
<td>File:</td>
<td><input type="file" name="File_1" size="30"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Submit"></td>
</tr>
</form>
</table>
</body>
</html>

CFMaBiSmAd
08-10-2007, 12:48 AM
Your form and the $_FILES['File_1'] array implement a HTTP upload, from the browser to the web server.

I assume you are using the FTP logic to transfer this to a different server? Because, since you have already uploaded this to the web server, if the web server and the FTP server share the same file space, you should just move the file instead of going through the trouble of FTP'ing it.

Some problems I see in the code -

1) It assumes that register globals are on. The following line of code won't work if register globals are off (php.net has recommend for more than a few years now to not write any new code that relies on register globals being on) -

if (isset($action) && $action == "submit")

2) Your $Filename variable in the following is relative to the connection you have to the FTP server -

$Filename = $FTP_Root . $_FILES['File_1']['name']; This would have a value like - "httpdocs/charbel/upload/somefile.ext". Putting this into the file_exists() function has no meaning.

You can use the ftp wrapper in the file_exists() function, but it would need to look like the following in order to refer to a file on the ftp server -

ftp://ftp.yourdomain.com/upload/somefile.ext // anonymous ftp

ftp://user:password@ftp.yourdomain.com/upload/somefile.ext // non-anonymous ftp

charbsk
08-13-2007, 09:16 PM
that worked great, thank you so much, take care:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum