I've been working on a script to move files from one ftp server to the server our website is on.
The script was working earlier but it wasn't working correctly with the re-naming of the files, so I fixed that, but now I get an error: Invalid argument for foreach() statement. I know why the arg is invalid, $file = ftp_nlist() is returning a bool false instead of the array it should. Now, this WAS working earlier, and I didn't change anything outside of the foreach loop. So I don't know what's up.
Here's the code, I'm stuped:
<?php
/**
* @author Xodus
* @copyright 2007
* FTP PUNKBUSTER SCREEN SHOT AUTO TRANSFER!
*/
$ftp_server = "69.65.32.219";
$ftp_user_name = "2574";
$ftp_user_pass = "*****";
$remoteSSPath = "/";
$destination_file = "./Unsorted/";
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result))
{
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
}
else
{
echo "Connected to $ftp_server, for user $ftp_user_name";
}
$file = ftp_nlist($conn_id, $remoteSSPath);
echo "<br>";
var_dump($file);
foreach ($file as $files)
{
$ispng = false;
$findMe = "png";
$ispng = strpos($files, $findMe);
$newName = rand(1, 1000000);
$newName .= ".png";
if ($ispng)
{
ftp_rename($conn_id, $files, $newName);
$get = ftp_get($conn_id, $destination_file . $newName, $newName, FTP_BINARY);
if (!$get)
{
echo "FTP download has failed downloading: $newName!<br>";
}
else
{
echo "Downloaded $files to $destination_file <br>";
echo "Deleting $files:<br>";
if (ftp_delete($conn_id, $newName))
{
echo "$newName Deleted! <br>";
}
else
echo "Error deleting file!";
}
}
else
{
echo "$files is not a .png file! - Not downloading <br>";
echo "Deleting $files:<br>";
if (ftp_delete($conn_id, $files))
{
echo "$files Deleted! <br>";
}
else
echo "Error deleting file!";
}
}
ftp_close($conn_id);
echo "<br>Closed FTP Session<br>";
?>
Any suggestions would be helpful.
Thanks,
Xodus