Guest kilbad Posted October 2, 2006 Share Posted October 2, 2006 I want to use PHP to rename all files in a particular directory with the extension .jpg/.JPG to numbers (with a jpg extension), starting with zero.For example, file1.JPGanotherFILE.jpg0872.JPGwould be renamed to..0.jpg1.jpg2.jpgCould someone help me with this script? I am not sure where to start.Thanks for the help!Brendan Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 2, 2006 Share Posted October 2, 2006 you'd probably want to use a simple combination of readdir() and rename()... also, make sure that the file permissions are such that your server will have access to rename the files. something like this should get you pointed in the right direction (i would also recommend padding your names with 0's to help with sorting. i my example, i'm going to create 3 digit names padded with 0:[code]<?php$dir = "path/to/directory";$files = array();if ($handle = opendir($dir)) { while (FALSE !== $file = readdir($handle)) { if ($file != '.' && $file != '..') $files[] = $file; }}for ($i = 0; $i < count($files); $i++) { $filename = sprintf("%03d", $i); rename("$dir/" . $files[$i], "$dir/" . $filename);}?>[/code]another option would be simply to copy() your files with the new name attached and then delete the old filename.hope this helps. Quote Link to comment Share on other sites More sharing options...
printf Posted October 2, 2006 Share Posted October 2, 2006 Just another way...[code=php:0]$dir = './some/path/';$num = 0;foreach ( glob ( $path . "{*.jpg,*.JPG}", GLOB_BRACE ) AS $file ){ rename ( $file, $dir . $num++ . 'jpg' );}[/code]me! Quote Link to comment Share on other sites More sharing options...
Guest kilbad Posted October 3, 2006 Share Posted October 3, 2006 First, let me thank both of you for your help. However, I have a follow-up question regarding [b]printf[/b]'s code..I don't want to simply use your code without understanding it completely.. so could you, or someone else, explain what is happening with the [i]foreach[/i] command (esp. with regard to the GLOB_BRACE).Just FYI, my modified code::[code]<?php$dir = '/mypath/';$num = 0;foreach ( glob ( $dir . "{*.jpg,*.JPG}", GLOB_BRACE ) AS $file ) {rename ( $file, $dir . $num++ . '.jpg' );}?>[/code]Thank you so much.Brendan Quote Link to comment Share on other sites More sharing options...
printf Posted October 3, 2006 Share Posted October 3, 2006 $variable = glob ();is the same asopendir ()readdir ()loop the directory adding all files to the $variable assigned to it, it return array () of files based on a wildcard (*?) match!closedir()GLOB_BRACEis used to match CASE, because GLOB is case sensitive, so you have to the function glob() to include both upper and lower case naming if you want both included! glob() is also 8X faster than read directory! read directory is better when you don't have a lot of memory to hold the directory listing in a array!me! Quote Link to comment Share on other sites More sharing options...
Guest kilbad Posted October 4, 2006 Share Posted October 4, 2006 Thank you for that explaination..I have another follow-up question:: [b]How can I get the list of file names to be renamed numerically in the order they are currently listed, not randomly..?[/b]for example, let's say I have..IMG_1943.JPGIMG_1944.JPGIMG_1945.JPGIMG_1990.JPGX.jpgI want to rename them to..IMG_1943.JPG --> 0.jpgIMG_1944.JPG --> 1.jpgIMG_1945.JPG --> 2.jpgIMG_1990.JPG --> 3.jpgX.jpg --> 4.jpgRight now when I run the script on a list of files it seems to rename them numerically in a random fashion.Thanks so much for the help printf.Brendan Quote Link to comment Share on other sites More sharing options...
printf Posted October 4, 2006 Share Posted October 4, 2006 Take glob out of the foreach() and then do your foreach renaming after sorting. That's odd, because glob() should sort by default, it does on both Windows and Linux, at least the servers I just tried it on, maybe you need to use | GLOB_NOSORT after GLOB_BRACE to get the right order. Anyway here is another way to do it, using sorting![code]<?// diretory to read $dir = './images/';// file extension$type = '.jpg';// starting number$start = 0;// run it$files = glob ( $dir . "{*.jpg,*.JPG}", GLOB_BRACE );// natrual sort, file namesif ( ! empty ( $files ) ){ natcasesort ( $files ); foreach ( $files AS $file ) { rename ( $file, $dir . $start++ . $type ); }}?>[/code]me! Quote Link to comment Share on other sites More sharing options...
Guest kilbad Posted October 5, 2006 Share Posted October 5, 2006 Ok, this is my final question regarding this topic...After the rename of more than 10 files, I get a list that sorts as follows...0.jpg1.jpg10.jpg11.jpg12.jpg2.jpg3.jpg4.jpg5.jpg6.jpg7.jpg8.jpg9.jpgHowever, I would like these files to sort numerically..My question: [b]Is this a case where padding with zeros is necessary (like obsidian mentioned)? If so, using printf's example, how do I do that, and how many zeros should I use?[/b]Thanks again so much! Sorry to have revisited this issue so many times!Brendan Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 5, 2006 Share Posted October 5, 2006 yes, padding with zeros would be necessary. basically, just decide what the MOST digits you will need is, and do this when you rename it:[code]<?php// simply replace this line:rename ( $file, $dir . $start++ . $type );// with this:rename ( $file, $dir . sprintf("%03d", $start++) . $type );?>[/code]that code will make it padded to 3 digits. simply change the '3' in the sprintf() above to any number of digits you're after. good luck! 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.