PHP - Move files to new folder
Posted: Mon Mar 04, 2013 3:14 pm
Hi everyone,
I've been struggling with this code for quite sometime and i'm wondering if anyone would have and suggestions on how to make the code work. Basically what needs to be done is for every file found, check to see if a folder exists, if not create it, then move all related files into that folder, deleting the original file after copying the file(s) to the folder.
the folder is created, but no files are copied into the folder. Any help would be appreciated! Thanks.
I've been struggling with this code for quite sometime and i'm wondering if anyone would have and suggestions on how to make the code work. Basically what needs to be done is for every file found, check to see if a folder exists, if not create it, then move all related files into that folder, deleting the original file after copying the file(s) to the folder.
the folder is created, but no files are copied into the folder. Any help would be appreciated! Thanks.
Code: Select all
<?php
preg_match_all('/([^\[]+)\[([^\]]+)\],?/', $attachments, $matches, PREG_SET_ORDER);
$attachments = array();
foreach ($matches as $file) {
$attachments[$file[1]] = $file[2];
}
foreach ($attachments as $file => $filename) {
$uploaddir = "mail/attachments/" . $file;
$casenumdir = "mail/attachments/" . $CaseNumber;
if(is_dir($casenumdir)==false){
mkdir("$casenumdir", 0700); // Create directory if it does not exist
}
if(is_dir($casenumdir.$file)==false){
chmod ($uploaddir, 0777);
if (copy($uploaddir,$casenumdir)) {
unlink($uploaddir);
}}else{ // rename the file if another one exist
$new_dir=$casenumdir.$file.time();
$file_temp = $uploaddir . $file;
rename($file_tmp,$new_dir) ;
}
fclose($uploaddir);
}
?>