PHP - Move files to new folder

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP - Move files to new folder

Post by cjkeane »

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.

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);
}
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP - Move files to new folder

Post by requinix »

Does $casenumdir need a trailing slash?
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP - Move files to new folder

Post by cjkeane »

yes it does. essentially, the $casenumdir would look like this: mail/attachments/021911-GA
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: PHP - Move files to new folder

Post by mecha_godzilla »

Hi,

As requinix has suggested, the problem here is that the filepath isn't being specified correctly.

So, a few points:

1. In your second foreach() loop you've defined a $filename variable which then isn't used in any other part of the script. Is that correct?

2. Where you have any lines that contain

Code: Select all

$casenumdir.$file
you need to make sure they read

Code: Select all

$casenumdir.'/'.$file
because otherwise the filepath will end up looking something like

Code: Select all

mail/attachments/021911-GAmydocument.pdf
3. A similar problem also arises where you're declaring the $new_dir variable, because the filepath will then become

Code: Select all

mail/attachments/021911-GAmydocument.pdf1362437360 
4. You have two variables for the temporary filename - $file_temp and $file_tmp - which might also be adding to your problems.

HTH,

Mecha Godzilla
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP - Move files to new folder

Post by cjkeane »

UPDATED: thanks for both of your responses. Your suggestions work, however there still seems to be a problem in my for loop somewhere

I have a new issue whereby one attachment is saved into the db correctly, however if there are multiple attachments, it only saves one to the db. i need attachments to be inserted in the db as casenumber/filename1.ext, casenumber/filename2.ext. Any help would be appreciated. It's like my for loop only catches one file. when i echo $filelist to the page, it displays multiple file names, but when i do the db insert, it only saves one. Any ideas on how to resolve this?

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;
		$newfiledir = "mail/attachments/" . $CaseNumber .'/'. $file;
		$each_file = $CaseNumber .'/'. $file;
	
       if(is_dir($casenumdir)==false){
           mkdir("$casenumdir", 0700);          // Create directory if it does not exist
        }
       if(file_exists($casenumdir.'/'.$file)==false){
            chmod ($uploaddir, 0777);
            copy($uploaddir,$newfiledir);
        }
        $allfiles = $CaseNumber .'/'. $file . "[" . $filename . "]" . ",";
	$filelistfinished = preg_replace("/,$/", "", $allfiles);
        echo $filelistfinished; 
        // displays multiple file attachments on the page correctly as: casenumber/testfile1.pdf[testfile1.pdf],casenumber/testfile2.pdf[testfile2.pdf],
       // but the db insert of $filelistfinished only inserts  casenumber/testfile1.pdf[testfile1.pdf],

}
?>
Post Reply