PDA

View Full Version : [php] copy files from folder A to folder B not working


Paul Ferrie
01-13-2007, 12:33 AM
Hi guys i am try to copy already uploaded files from temp folder to newly created folder

$moveID = mysql_insert_id();
$movePath = "../media/events/".$moveID."/";
if (!file_exists($movePath)) {
mkdir($movePath,0777);
chmod($movePath,0777);
}

//----------------------------------//
$tempPath = "../temp";
if ($handle = opendir($tempPath)) {
while (false !== ($file1 = readdir($handle))) {
if ($file1 != "." && $file1 != "..") {
rename($tempPath."/".$file1, $movePath.$file1);
$del=unlink($tempPath."/".$file1);

}
}
closedir($handle);
}

If the folder is already created(exsistint on server) and has the chmod set to 777 it works fine. The files want copy over to the new folder if created using mkdir

Anyone?
Thanks

jggretton
01-13-2007, 02:01 AM
As far as I remember there is a problem with the mkdir function in PHP. PHP sets the user of the folder to a strange value, and so you can't write to the folder later.

In the past I've had to use ftp to make new folders and write to them:


function FtpMkdir($path, $newDir) {
$server='.....'; // ftp server
$connection = ftp_connect($server); // connection
// login to ftp server
$user = "........";
$pass = "******";
$result = ftp_login($connection, $user, $pass);
// check if connection was made
if ((!$connection) || (!$result)) {
return false;
exit();
} else {
ftp_chdir($connection, $path); // go to destination dir
if(ftp_mkdir($connection, $newDir)) { // create directory
ftp_site($connection, "CHMOD 777 $path/$newDir") or die("FTP SITE CMD failed.");
return $newDir;
} else {
return false;
}
ftp_close($connection); // close connection
}
}

Paul Ferrie
01-13-2007, 10:32 AM
Is this correct?
The folder isn't being created now.

if (!file_exists($movePath)) {
$path="httpdocs/media/events/";
$dir="httpdocs/media/events/".$moveID."/";
FtpMkdir($path,$dir);
}


Thanks

jggretton
01-14-2007, 06:24 PM
The path is probably right, but the directory should be:


$dir = $moveID


To check the path is correct, i'd suggest logging in via ftp yourself. If you can see "httpdocs" when you first log on, then this should be correct.

Good luck,

James