
August 16th, 2006, 11:37 AM
|
|
Contributing User
|
|
Join Date: Sep 2004
Posts: 112
Time spent in forums: 1 Day 6 h 29 m 12 sec
Reputation Power: 5
|
|
|
Moving files on FTP server with PHP
Hello,
I am trying to write a php script that gets the files from a FTP server and then once transferred successfully moves those files to another folder so they aren't processed again.
I can't move, or rename them via php, can anyone help?
I have tried using the commands: mv and rename but with no luck
I have permissions to rename them as I have done this successfully manually.
Here is my script so far
PHP Code:
// 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!<br>";
exit;
} else {
echo "Connected to $ftp_server";
}
// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");
foreach($contents as $key => $file)
{
$extension = substr(strrchr($file, "."), 1);
echo "KEY $key => $file $extension<br>";
if($extension == "zip")
{
// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $file, $file, FTP_BINARY))
{
echo "Successfully written to $file\n<br>";
// move file into OLD folder
movefile($file, $conn_id);
}
else
{
echo "There was a problem\n<br>";
}
}
}
// close the FTP stream
ftp_close($conn_id);
function movefile($filename, $conn_id)
{
$command = "rename ".$filename." Old/".$filename;
echo "in rename zip file $command<br>";
// execute command
if (ftp_exec($conn_id, $command))
{
echo "$command executed successfully\n<br>";
$output = true;
}
else
{
echo "could not execute $command\n<br>";
$output = false;
}
return $output;
}
|