I wrote a PHP script that allows users to upload files to a FTP Server, the script works perfectly fine and files get uploaded with no problem. The issue i'm having is checking if the same file already exist on the server, when i upload the same file, it starts to upload and then returns with the error message and doesn't upload to the server like I want it to. My question is if there is a way to check before the upload process begins instead of wasting time and having to come back with the error message:

The problem I believe is at this point in the code:

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

if (is_array($contents))
{
if (in_array("$Filename", $contents))
{
echo "The file <font color='red'>$Filename</font> already exists on server<br>";
exit;
}
}
$upload = ftp_put($Connect, $destination_file, $file, FTP_BINARY);


Full PHP Code:

PHP Code:
<?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 ($Connecttrue );
    
    
/*
    if (!passive){
        echo "Failed to enter passive mode.<br>";
    }
    else {
        echo "Entered passive mode.<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'];
    
$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'];

    
$contents ftp_nlist($Connect".");
    
    if (
is_array($contents)) 
    { 
        if (
in_array("$Filename"$contents)) 
        { 
        echo 
"The file <font color='red'>$Filename</font> already exists on server<br>";
        
ftp_close($Connect);
        exit;
        }
     }
        
$upload ftp_put($Connect$destination_file$fileFTP_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);
}

?>