HaLo2FrEeEk Posted December 14, 2009 Share Posted December 14, 2009 I need to know how to rename files in a directory using php, specifically just files that have a space in the filename. I want to replace those spaces with underscores. First I need to know to to get a list of just the filenames that have spaces in them. What's the best way to do this? I'm thinking get a list of all the filenames and foreach the array, then split each filename into each character and check if that character is a space, then add that filename to another array, then foreach that other array and str_replace() each filename to replace spaces with the desired charcter. Is there a better way? Quote Link to comment https://forums.phpfreaks.com/topic/185115-renaming-all-files-in-a-directory-replacing-spaces-with-underscore/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2009 Share Posted December 14, 2009 If the str_replace() of spaces to underscores is not equal to the original, then the file name had one or more spaces in it. Quote Link to comment https://forums.phpfreaks.com/topic/185115-renaming-all-files-in-a-directory-replacing-spaces-with-underscore/#findComment-977166 Share on other sites More sharing options...
mikesta707 Posted December 14, 2009 Share Posted December 14, 2009 depending on what files you want to rename (if its all, only gifs, etc.) you can use the rename() function in combination with glob(). something like this would work for all file types (assuming you are in the same directory $files = glob("*.*");//get all files in directory (note, if you need to set //the directory, you should so something like glob("/path/to/*.*"); foreach($files as $file){ //$file is each individual file //replace spaces with underscores $new_file = str_replace(" ", "_", $file); rename($file, $new_file);//rename file } a little side note, you can add an if statement using the logic that PFMaBiSmAd suggested to only rename files with spaces if you want Quote Link to comment https://forums.phpfreaks.com/topic/185115-renaming-all-files-in-a-directory-replacing-spaces-with-underscore/#findComment-977169 Share on other sites More sharing options...
PFMaBiSmAd Posted December 14, 2009 Share Posted December 14, 2009 Same/similar to above ^^^ - <?php $dir = "folder/*.*"; // where and what files to operate on $files = glob($dir); foreach($files as $file){ if($file != $ren = str_replace(" ","_",$file)){ // there was a space rename($file,$ren); echo "Renaming: [$file] to [$ren]<br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/185115-renaming-all-files-in-a-directory-replacing-spaces-with-underscore/#findComment-977172 Share on other sites More sharing options...
HaLo2FrEeEk Posted December 15, 2009 Author Share Posted December 15, 2009 Well I'm going to be processing all files with .wma, .mp3, and .wav extensions, would that be similar enough code? I can do this: <?php $path = "./"; $handle = opendir($path); $allow = array("wma", "mp3", "wav"); while(($file = $readdir($handle) != false) { $ext = substr(strtolower($file), strrpos($file, ".")+1, strlen($file)); if(filetype($path.$file) == file && in_array($ext, $allow)) { // Only adds the files to the array if the extension is in the $allow array $files[] = $file; } } foreach($files as $file) { if($file != ($ren = str_replace(" ", "_", $file))) { rename($file, $ren); echo "File ".$file." renamed to ".$ren; } else { echo "No spaces in filename, skipping"; } echo "<br>"; } ?> That should get the job done, right? And I only need to do this once, not like, on a schedule or anything. It's just because I have files already there that have spaces that need to not have spaces, future uploaded files will have no spaces, I just don't feel like manually renaming hundreds of files. Quote Link to comment https://forums.phpfreaks.com/topic/185115-renaming-all-files-in-a-directory-replacing-spaces-with-underscore/#findComment-977651 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.