ftp via php scrip check if file exist before uploading proce

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
charbsk
Forum Newbie
Posts: 7
Joined: Thu Aug 09, 2007 5:29 pm

ftp via php scrip check if file exist before uploading proce

Post by charbsk »

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:

Code: Select all

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

?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

With FTP, the only way I can imagine checking if a file exists, other than retrieving a directory listing, would be to first attempt to retrieve the file. If the file is not there then you can upload the file.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

This is the last time you get to duplicate your thread charbsk.Image
Locked