Page 1 of 1

check if file exists on ftp server problems

Posted: Mon Aug 13, 2007 4:29 pm
by charbsk
I made a PHP script that allows me to upload files to a FTP server, the script works perfect and uploads the files with no problems. My problem is that I have this peice of code checking if the file exists on the FTP server but I keep getting a msg saying that it doesn't exist, I'm not sure if my code is right or if i'm missing something or if I'm not using the code correctly. This is the piece of code that I'm unsure of :

if (file_exists($Filename))
{
echo "The file $Filename exists<BR>";
}
else
{
echo "The file $Filename does not exist<BR>";
}

Any help would be appreciated. Thanks

Here is the 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);
	
	//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 $contents
	print_r($contents); 
	
	// 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 = $_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'];

    // If the file was successfully uploaded
	$upload = ftp_put($Connect, $destination_file, $file, FTP_BINARY);

	 if (file_exists($Filename))
      {
          echo "The file $Filename exists<BR>";
      }
      else
      {
          echo "The file $Filename does not exist<BR>";
      }
	
	if (!$upload)
	{
        // Show success message
        echo "There was a problem uploading $destination_file";
    }
    else
    {
        // Else show error message
        echo "Successfully uploaded $Filename";
		
    }
	ftp_close($Connect);
}

?>
Form Code:

Code: Select all

<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 Type:</td>
<td><select name="dir">
	<option value="-1">File Type</option>
	<option value="/httpdocs/charbel/upload/">Image</option>
	<option value="/httpdocs/charbel/upload/">PDF</option>
	<option value="/httpdocs/charbel/upload/">MP3</option>
	<option value="/httpdocs/charbel/upload/">Video</option>
	</select>
</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>

Posted: Mon Aug 13, 2007 5:00 pm
by feyd
I have to ask why this required a new thread. Please explain.

Posted: Mon Aug 13, 2007 6:23 pm
by charbsk
I tried :

if (file_exists($FTP_Root.$Filename))
{

echo "The file $Filename exists";

}
else
{

echo "The file $Filename does not exists";

}

The error i get is :

arse error: parse error, unexpected $ in /var/www/vhosts/evolution.tv/httpdocs/charbel/test.php on line 143

$FTP_Root is the path, and i want to add the filename next to the path, but it won't let me, guess its syntax error.

I've also tried doing an ftp_nlist of the directory and displaying the array to match the filename below:

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

for ($x=0; $x < $contents; $x++)
{

if ($Filename == $contents[$x])

{

echo "The file $Filename exists";
exit;

}

else

{

echo "The file $Filename does not exist";
exit;

}

}

I know i'm in the right directory at this point but with that, the first time i upload the file it comes back and says that file exists, when i go to upload another file, it comes back saying it doesn't exist. Not sure if i'm using the syntax correct in matching the file to the listing of the array.

Any help on either that will help fix the situation would be appreciated.