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);
}
?>
<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>