I made a PHP script that allows user to upload files to an FTP Server. The script works perfect and uploads files fine. I added a piece of code with the ftp_nlist to put a list of files into an array from the upload directory to check if the file already exist if they upload the same file, the first time i uploaded a file it worked fine and i tried to upload the same file and I got the correct Error, but when I go to upload a new file, it starts uploading, but comes back with no success msg and doesn't upload the file to the FTP server. Below is my code, any help would be appreciated.
<?php
// FTP Configuration
$FTP_User = "$username";
$FTP_Pass = "$password";
$FTP_Host = "###";
$FTP_Root = "$dir";
// If the form was submitted
if (isset($action) && $action == "submit") {
if ($dir == "-1")
{
echo "Error: Please choose a File Type to upload";
exit;
}
// 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);
if (!$login)
{
echo "Error: Could not log on as $FTP_User<br>";
ftp_quit($Connect);
exit;
}
echo "Logged in as $FTP_User<br>";
//Turns passive mode on
$passive = ftp_pasv ($Connect, true );
// check upload status
if (!passive){
echo "Failed to enter passive mode.<br>";
}
else {
echo "Entered passive mode.<br>";
}
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";
// Set the filename to be uploaded
$Filename = $_FILES['File_1']['name'];
----- THE ERROR IS IN THE FOLLOWING CODE --------------
$contents = ftp_nlist($Connect, ".");
if ($contents[0] != "")
{
if (in_array("$Filename", $contents))
{
echo "The file $Filename already exists on server<br>";
exit;
}
}
else
{
$myFile = $_FILES['File_1'];
$destination_file = $FTP_ROOT.$_FILES['File_1']['name'];
// Set the local resource (the file that will be uploaded)
$file = $myFile['tmp_name'];
// If the file was successfully uploaded
$upload = ftp_put($Connect, $destination_file, $file, FTP_BINARY);
if (!$upload)
{
// Show success message
echo "There was a problem uploading $Filename<BR>";
}
else
{
// Else show error message
echo "Successfully uploaded $Filename<BR>";
}
$contents = ftp_nlist($Connect, ".");
if (in_array("$Filename", $contents))
{
echo "The File $Filename exists<br>";
}
else
{
echo "The File $Filename does not exist<br>";
}
}
ftp_close($Connect);
}
?>