PHP: Rename all files in folder

I have a lot of files in two folders, I need a script to rename them [php].

In folder one /images they are all in a form such as "3245 green forest.jpg" and I want them all renamed to, for example "3245_green_forest.jpg".

In folder two /thumbs I want the same starting file ("3245 green forest.jpg") to be renamed to "3245_green_forest_thumb.jpg"

LVL 11
level9wizardAsked:
Who is Participating?
 
EMB01Commented:
I think this is how to replace spaces... Try it out...
<?php
 
// change this variable accordingly
$dir = "/my/directory";
 
// put all files in a directory into an array
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $files[] = $file;
        }
    }
    closedir($handle);
}
 
// strip .jpg from name
foreach ($files as $file) {
    preg_replace("/.jpg/", "", $file);
}
 
// replace spaces with underscores
foreach ($files as $file) {
    preg_replace("/ /", "_", $file);
}
 
// rename file
foreach ($files as $file) {
    rename($file, $file . "_thumb.jpg");
}
 
?>

Open in new window

0
 
EMB01Commented:
You will want to do this:

- List all files in directory
- Put all files into an array
- Rename all files with new value

I'll post up a code you can try.
0
 
EMB01Commented:
Here's the code you can try. Be sure to change the $dir variable. Let me know what happens.

The script should:

- Open directory
- Read all files
- Put files into array
- Remove .jpg from all files in array
- Append and update new files names from array
<?php
 
// change this variable accordingly
$dir = "/my/directory";
 
// put all files in a directory into an array
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $files[] = $file;
        }
    }
    closedir($handle);
}
 
// strip .jpg from name
foreach ($files as $file) {
    preg_replace("/.jpg/", "", $file);
}
 
// rename file
foreach ($files as $file) {
    rename($file, $file . "_thumb.gif");
}
 
?>

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
EMB01Commented:
Sorry, you'd need to use this for .JPG (not .GIF as I had previously).
<?php
 
// change this variable accordingly
$dir = "/my/directory";
 
// put all files in a directory into an array
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $files[] = $file;
        }
    }
    closedir($handle);
}
 
// strip .jpg from name
foreach ($files as $file) {
    preg_replace("/.jpg/", "", $file);
}
 
// rename file
foreach ($files as $file) {
    rename($file, $file . "_thumb.jpg");
}
 
?>

Open in new window

0
 
level9wizardAuthor Commented:
This does not do what I asked above. It should replace spaces with _

0
 
level9wizardAuthor Commented:
Hmm, that wasn't it either. In the end I made one myself:

<?php
$path = "c:\\wamp\\www\\images";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != "..") {
        echo "".$path . "\\".str_replace(" ","_",$file);
        rename($path."\\".$file, $path . "\\".str_replace(" ","_",$file));
        $i++;
    }
}
closedir($dh);
?>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.