Am trying to get my script to read in a directory and search for a a file, if the file doesnt exist it should print"that it doesn't exist" then add Notinfolder in an array. Please assist

<?php
$connection = ftp_connect($server);

$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);

if (!$connection || !$login) { die('Connection attempt failed!'); }

echo "Connected"."<br>";
$newcontents = ftp_chdir($connection, 'public_html');
$newcontents = ftp_nlist($connection, ".");
$cart = array();
$datedone=date("m.d.y");
foreach ($newcontents as $v){
if($v == "test1.zip"){
echo "True";
$cart[0]="test1.zip";
break;
}
elseif ($v<>"test1.zip")
{
echo "Test1 doesn't exist";
$cart[0]="Failed:".$datedone;
break;
}
}
echo "<br>";
print_r(array_values($cart));
ftp_close($connection);
?>

Recommended Answers

All 5 Replies

What is the problem you are having?

The problem am having is that when the file is on the ftp server it still reads:"Test1 doesn't exist".Is there any other way I can achieve the same results to check if the file exists and put my results in an array for later use.

Can you print_r($newcontents); before the loop and show here what it contains?

The problem am having is that when the file is on the ftp server it still reads:"Test1 doesn't exist".Is there any other way I can achieve the same results to check if the file exists and put my results in an array for later use.

I managed to try the following and it works. If anyone has a better way or quicker way I would appreciate any feedback.

if (in_array("test1.zip", $newcontents)) {
    echo "Got Test";
}
else
{
echo "Don't have Test";
}
echo "<br>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.