kasztelan Posted January 11, 2010 Share Posted January 11, 2010 hey all i wrote upload script and though it is working fine i have a problem that i can't solve myself. lets say i have over 1000 files in dir1/ and i want to move them to dir2/. BUT i want to limit the number of files in directory to 10. so i'd like to have 10 files in dir2/ another 10 in dir2/1/ then dir2/2/, etc. i tried to write a scrip that would do that but it's not working too well. can someone help me fix this? (mysql functions are from php-fusion) $MAX_FILES_IN_DIR = 10; $basedir = "dir2/"; $counter = 1; $result = dbquery("SELECT * FROM table"); while($data = dbarray($result)) { $filecount = count(glob("" . $basedir . "*.*")); if($filecount <= $MAX_FILES_IN_DIR) $dir_level = 0; if($dir_level > 0 && count(glob("" . $basedir.$dir_level."/" . "*.*")) <= '$MAX_FILES_IN_DIR') {;} else { while($counter) { $upload_dir_new = $basedir.$counter."/"; if (!is_dir($upload_dir_new)) {mkdir($upload_dir_new); chmod($upload_dir_new,0755); } if (count(glob("" . $upload_dir_new . "*.*")) <= '$MAX_FILES_IN_DIR') {$dir_level = $counter; break 2;} if (count(glob("" . $upload_dir_new . "*.*")) > '$MAX_FILES_IN_DIR') {$counter++;} } } if($dir_level > '0') {$upload_dir = $basedir.$dir_level."/";} else {$upload_dir = $basedir;} $filename = "dir1/".$data['file']; $filename2 = $upload_dir.$data['file']; if (file_exists($filename)) { copy($filename,$filename2); } else { echo "The file $filename <font color=red>does not exist</font>"; } } Quote Link to comment Share on other sites More sharing options...
Catfish Posted January 11, 2010 Share Posted January 11, 2010 When I want to do things like this, I usually will divide the total number of items by the restriction amount and then make a loop that runs that many time. Example: if you have 100 files in directory, divide it by 10 (you will need to check for a remainder also to allow for say, 101, 102, 105 files etc) and you would get a result of 10 loops to be done (or 11 if there is a remainder). It's how I did pagination for my database browser. Quote Link to comment Share on other sites More sharing options...
ignace Posted January 11, 2010 Share Posted January 11, 2010 This should get the job done: function trigerrexit($message) { trigger_error($message); exit(0); } $directory = implode(DIRECTORY_SEPARATOR, array('path', 'to', 'dir2')); if (!is_directory($directory) || !is_readable($directory)) { trigerrexit("$directory is not a directory or is not readable."); } $directoryFileCount = array(); $directoryFileIndex = 0; $directoryPointer = $directory . DIRECTORY_SEPARATOR; // assuming $files are already read from directory1 foreach ($files as $file) { if (empty($directoryFileCount)) { $directoryFileCount[$directoryFileIndex] = 0; $directoryPointer = $directory . DIRECTORY_SEPARATOR . $directoryFileIndex . DIRECTORY_SEPARATOR; if (!mkdir($directoryPointer)) { trigerrexit("Could not create directory $directoryPointer"); } } else if (10 > $directoryFileCount[$directoryFileIndex]) { if (!copy($file, $directoryPointer . basename($file))) { trigerrexit("$file could not be copied to $directoryPointer"); } $directoryFileCount[$directoryFileIndex]++; } else { $directoryFileIndex++; $directoryFileCount[$directoryFileIndex] = 0; $directoryPointer = $directory . DIRECTORY_SEPARATOR . $directoryFileIndex . DIRECTORY_SEPARATOR; if (!mkdir($directoryPointer)) { trigerrexit("Could not create directory $directoryPointer"); } } } Quote Link to comment Share on other sites More sharing options...
kasztelan Posted January 11, 2010 Author Share Posted January 11, 2010 thanks for the help ignace, after i changed is_directory to is_dir it worked like a charm! i moved all the files. i'd be great if you could help me with something similar but for file submission page. only one file will be uploaded if it has any meaning cheers Quote Link to comment Share on other sites More sharing options...
kasztelan Posted January 12, 2010 Author Share Posted January 12, 2010 ok i've managed to do it myself. thanks again! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.