...

View Full Version : in_array problem



charbsk
08-14-2007, 02:13 AM
I have a PHP script that allows users to upload files to an FTP Server. The script works fine and uploads the files with no problem. I have the ftp_nlist function listing the files into an array called $contents, and I'm trying to check if the file being uploaded already exist on the server, the paths are correct as well. After uploading 1 file and trying to upload it again, I get the correct Error message, but when I go to upload another file, the script starts to upload the file, but returns with no confirmation that it exists as well as doesn't upload to the FTP Server. Below is the 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'];

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

?>

The problem is in this section of 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);
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum