ghurty Posted January 16, 2010 Share Posted January 16, 2010 How would I go about writing a php script that every time it runs, it will move 30 files out of a particular directory, into a different directory. The folder will be the current date (ex: 012410 or 01242010). The folder will have a few hundred files. I want all the files moved into a different directory, but due to different reasons, I would not want them moved all at once, rather lets say thirty at a time. So I would want it that each time the script is run, it will move 30 of the files over. However, I dont want it that if the directory is empty (or has less then 30 files), it will cause the server to crash (or use tons of resources) because it is bumping into errors. Thank You Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/ Share on other sites More sharing options...
MadTechie Posted January 16, 2010 Share Posted January 16, 2010 Why not just FTP them over ? Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996280 Share on other sites More sharing options...
ghurty Posted January 17, 2010 Author Share Posted January 17, 2010 Because I need this done automatically. I am planning on using a cron job to run it ever half hour. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996291 Share on other sites More sharing options...
Catfish Posted January 17, 2010 Share Posted January 17, 2010 here is some pseudocode: use some filesystem functions to check number of files in directory. if more than 30 files in folder: use for loop to loop around 30 times move files from directory to destination directory, one file per loop of for loop. (verify each move else output warning/error) exit script with success message, show user what happened. else: do not do anything. exit script showing user there is not enough files in the directory. here are some links to php manual pages that will help: http://www.php.net/manual/en/refs.fileprocess.file.php - Filesystem functions http://www.php.net/manual/en/function.scandir.php - scandir() http://www.php.net/manual/en/control-structures.for.php - for() control structure http://www.php.net/manual/en/function.rename.php - rename() - also used to move files Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996300 Share on other sites More sharing options...
$Three3 Posted January 17, 2010 Share Posted January 17, 2010 To check whether the directory you are using for example the current date, you could use the scandir() function to test whether there are 30 or more files in the directory. So for example you could use this to test: <?php $directory = '012410' ; $directoryScan = scandir($directory) ; $numFiles = count($directoryScan) - 1 ; if ($numFiles >= 30) { echo "There are 30 or more files in the directory: $directory" ; } else { echo "There are no files to move today." ; } ?> This code will just check to see if there are more than 30 files on the particular folder you want to check. Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996302 Share on other sites More sharing options...
Catfish Posted January 17, 2010 Share Posted January 17, 2010 Your script would also need to create destination directories, so you would need to ensure the file permissions of those directories are correct and maintain the naming control of the destination directories. Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996303 Share on other sites More sharing options...
salathe Posted January 17, 2010 Share Posted January 17, 2010 Perhaps I'm thinking too far outside of the box, but if this is really just a matter of moving (files in) a directory when a CRON runs, can't you just use shell commands to get the job done (thinking mv)? Needing to only move 30 files at a time is a very strange requirement, could you elaborate on exactly why that's a necessity? Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996309 Share on other sites More sharing options...
laffin Posted January 17, 2010 Share Posted January 17, 2010 how about make it simple rename($oldfolder,$newfolder); u can rename folders into different directories rename('cache/current','backup/yesterday') on windows beware, you cant move folders across drives. Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996310 Share on other sites More sharing options...
ghurty Posted January 17, 2010 Author Share Posted January 17, 2010 The files will always be moved into the same directory. I have an app that automatically process files in that directory. However the app has been known to crash if to many files are added in at once. So all I am trying to do is slow down the transfer of files into it into a manageable rate. The origination folder can have a few hundred files in it. Eventually I want all the files moved into the other folder, but not all at once. So I thought the best way was to have a cron job run a php file that copies 30 files at once, and I would have the cron job run every few minutes. If you have a better suggestion, I would love to hear it. Also how would I tell that the variable $origdirectory should equal todays date? Thank You Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996311 Share on other sites More sharing options...
laffin Posted January 17, 2010 Share Posted January 17, 2010 <?php $origdirectory=date('mdy').'/'; $destdirectory='process/'; if(is_dir($origdirectory)) { $dh=opendir($origdirectory); $ctr=0; while($ctr<30 && $file=readdir($dh)) { if(is_file($origdirectory.$file)) { rename($origdirectory.$file,$desdirectory.$file); $ctr++; } } closedir($dh); if(!$ctr) rmdir($origdirectory); ?> Edit: added file check, directory check, and orginal directory removal Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996314 Share on other sites More sharing options...
$Three3 Posted January 17, 2010 Share Posted January 17, 2010 Hey give this code a shot. It will move all files in the variable $directory to the NewDirectory only if there are 30 or more files in the $directory variable. The $directory variable will hold the current date for each day in the format of 01162010. If there are only a couple hundred files in the $directory variable it will not over run your server. I just moved 147 files in about 2 seconds with this same script. <<?php $directory = date('djY') ; $directoryScan = scandir($directory) ; $numFiles = count($directoryScan) - 1 ; if ($numFiles >= 30) { foreach ($directoryScan as $value) { if ((is_file($value)) AND (substr($item, 0, 1) != '.')) { rename("$directory/$value" , "NewFolder/$value") ; } } echo "There are 30 or more files in the directory: $directory<br /><br /> The files have been moved successfully." ; } else { echo "There are no files to move today." ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996317 Share on other sites More sharing options...
MadTechie Posted January 17, 2010 Share Posted January 17, 2010 Because I need this done automatically. I am planning on using a cron job to run it ever half hour. Erm.. Okay.. and whats that got to do with FTP ? Also whats your preferred file transfer method a push or a pull ? Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996321 Share on other sites More sharing options...
laffin Posted January 17, 2010 Share Posted January 17, 2010 OP never mentioned FTP, just transfer, so it is assumed he is working on a local system structure. Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996323 Share on other sites More sharing options...
ghurty Posted January 17, 2010 Author Share Posted January 17, 2010 Regarding FTP, I am trying to transfer from one directory to another on the same server. I also need it to be completely automatic. $Three3 - From what I understand from your script, it will copy all the files at once, as long as there are more the 30 files there. That is not what I need. What I am trying to complete is the following scenario: Origination folder has for example 200 files. I want it that the script runs once, moves 30 files. Two minutes later, the cron job runs again, so another 30 files are moved. This continues every two minutes until all the files are moved over. If I have more then 30 files moved into the new directory at a time, the app that is monitoring that directory will crash. So I only want thirty files moved over at once. Thank You Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996336 Share on other sites More sharing options...
laffin Posted January 17, 2010 Share Posted January 17, 2010 Than check my post which was above $Three's. It does just that. move 30 files at a time, when no more files are in the directory, it removes the directory. however, it doesnt recurse thru sub-directories (only does the top level) Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996339 Share on other sites More sharing options...
MadTechie Posted January 17, 2010 Share Posted January 17, 2010 the app that is monitoring that directory will crash. Ohhh.. i thought you want moving from a buggy server.. call app provider.. tell them to fix it! try this.. (its very basic) <?php $folder = date("mdY"); $count = 30; $target = "target/$folder/"; mkdir($target); mvr("source/$folder/",$target); function mvr($dir,$target){ global $count; if(!$dh = @opendir($dir)) return; while (false !== ($file = readdir($dh))){ if($file == '.' || $file == '..') continue; if(is_dir($dir.'/'.$file)){ mkdir($target.$file); mvr($dir.'/'.$file.'/', $target.$file.'/'); rmdir($dir.$file); }else{ if($count==0) return; rename($dir.$file,$target.$file); $count--; } } closedir($dh); } ?> EDIT: oops updated $count to 30 Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996358 Share on other sites More sharing options...
ghurty Posted January 17, 2010 Author Share Posted January 17, 2010 laffin - If lets say there are fifty files will it do thirty and then twenty, or will it stop when the number of files remaining is not divisible by thirty? Does the folder get processed from reletive to site root, or from the diretroy where the php file is located? Also, I am getting a parse error on line 18. Thank You. Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996360 Share on other sites More sharing options...
laffin Posted January 17, 2010 Share Posted January 17, 2010 last lines should have been if(!$ctr) rmdir($origdirectory); } ?> the script runs from the PWD (Present Working Directory) if running from CRON, I would make them full path directories $origdirectory='/my/logs/' . date('mdy').'/'; $destdirectory='/my/working/process/'; MadTechie's code is approx the same (cept his is recursive, it handles sub-folders) you would have to change these lines $target = "/my/working/folder/"; mvr("/my/logs/$folder/",$target); Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996373 Share on other sites More sharing options...
ghurty Posted January 17, 2010 Author Share Posted January 17, 2010 I am trying your script: In the root I have the php file. It is copying the file out, but instead of to the process folder, it is copying into the root directory (the same directory that I have the php file in). $destdirectory='process/'; Thank You Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996379 Share on other sites More sharing options...
ghurty Posted January 17, 2010 Author Share Posted January 17, 2010 I figured it out. Once you had it with a "t": $destdirectory='process/'; The other time without a "t": rename($origdirectory.$file,$desdirectory.$file); I fixed it and it worked, Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996380 Share on other sites More sharing options...
ghurty Posted January 17, 2010 Author Share Posted January 17, 2010 I have the files stored in a path not part of the website: The PHP file is in : /var/www/html The orig folder is in:/orig/ The destination folder is in: /var/spool/test So I have it: $origdirectory='/orig/' . date('mdy').'/'; $destdirectory='/var/spool/test'; I run it, and the files dont go anywhere. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996387 Share on other sites More sharing options...
Catfish Posted January 17, 2010 Share Posted January 17, 2010 put the call to rename() in an if() structure and on failure make it output an error that the file move failed. sounds like permissions in /var/spool/test may be incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/188730-how-to-have-a-script-move-30-files-every-time-it-is-run/#findComment-996413 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.