ftp via php script check if file exist before uploading proce

charbsk

207.***.***.***
1,081 days ago

ftp via php script check if file exist before uploading proce

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>";
ftp_close($Connect);
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 ($Connect, true );

/*
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, $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);
}

?>

Bill

80.***.***.***
1,081 days ago
Since you say you want it to go through, you can append a timestamp to the filename (time()) so that it is always unique.

Otherwise you'd have to use what we'd refer to as Ajax, which is this case would work out to having a client side Javascript handle the Submit button and make a call to a backend script that authorizes a normal submit if it determines that the filename doesn't exist in the upload folder.

Bill

80.***.***.***
1,081 days ago
Welcome to the forum, by the way. :)