Bulk Rename Files in a Folder - PHP

Bulk Rename Files in a Folder - PHP

Yeah, just open the directory and create a loop to access all images and rename them, like:

<?php

if ($handle = opendir('/path/to/files')) {
while (false !== ($fileName = readdir($handle))) {
$newName = str_replace("SKU#","",$fileName);
rename($fileName, $newName);
}
closedir($handle);
}
?>

References:

http://php.net/manual/en/function.rename.php

http://php.net/manual/en/function.readdir.php

http://php.net/manual/en/function.str-replace.php

rename all the files in a folder

You don't need to POST name

glob is return you every files in folder with path // example /basic_images/test.jpg

then you just do foreach to loop over files, and update its name.

$path = "../basic_images/";
$directory = glob($path,"*.*");
foreach($directory as $file){
$ext = pathinfo($file, PATHINFO_EXTENSION);
$newName = uniqid().$ext;
rename($file, $path.$newName);
}

read more about glob : http://php.net/manual/en/function.glob.php

Rename multiple files without overwriting one with another in php

I've created my own renaming multiple folder file names.

This function prevent overwiting as I explained and asked in the question (just at the top).

function rename (array $names): void {
$dirname = '...';
$files = File::folder($dirname); // 'File' is my own static class.

foreach ($names as $key => $name) {
$names[$key] = (basename($name) .'.'. File::extension($key));
// 'File' is my own static class.
}

$duplicates = array(); // for avoiding overwriting
foreach ($files as $key => $file) {
$duplicates[$key] = tempnam(sys_get_temp_dir(), '');
copy($file, $duplicates[$key]);
}

foreach ($names as $key => $name) {
if (isset($files[$key])) { // for preventing non-existent input filename
// remove file if its name isn't used for renaming
if (is_file($files[$key]) && !in_array($key, $names)) {
unlink($files[$key]);
}
rename($duplicates[$key], $dirname .'/'. $names[$key]);
}
}
}

rename(array(
"Maria.php" => "Ioana",
"Bianca.php" => "Cristina",
"Ioana.php" => "Daniela"
)); // Your new names don't have their extension, because you can't change the extension.

How can I bulk rename files in a photo folder with sequential numbers

You can use php glob and rename functions:

$num = 1;
foreach (glob("/path/to/images/*.jpg") as $filename) {
$fileNoExtension = basename($filename, ".jpg");
rename ($filename, "$fileNoExtension{$num}.jpg");
$num++;
}

PHP bulk rename, with more than one renaming function

You need to escape spaces. Try renaming directly using:

<?php

if ($handle = opendir('/Users/username/Documents/School')) {

while (false !== ($file_name = readdir($handle))) {
$to_lower = strtolower($file_name);
$add_dashes = str_replace(" ", "-", $to_lower);
exec("mv ".escapeshellarg($file_name). " ". $to_lower);
}

closedir($handle);
}
?>

rename all files and folder ftp in php language

If you have:

+ folder
- file1
- file2
+ folder with space
- file with space

... you currently first rename /folder with space to /folder_with_space.

And then you try to rename /folder with space/file with space to /folder with space/file_with_space. But that file does not exist anymore.

The easiest solution is to actually first rename children, then parent:

    $contents = ftp_nlist($resource, $thisPath);

// Recursive Part
foreach ($contents As $file) {
$theList = self::ftp_nlistr($resource, $file, $theList, FALSE);
}

$theList[] = $thisPath;

return $theList;

Renaming all files within different sub directories

If you are trying to lowercase all file names you can try this:

Using this filesystem:

Josh@laptop:~$ find josh
josh
josh/A
josh/B
josh/f1
josh/f2
josh/is
josh/is/awesome
josh/is/awesome/e
josh/is/awesome/t

Code:

<?php
$app_dir = 'josh';
$dir = new RecursiveDirectoryIterator($app_dir, FilesystemIterator::SKIP_DOTS);
$iter = new RecursiveIteratorIterator($dir);
foreach ($iter as $file) {
if ($file != strtolower($file)) {
rename($file, strtolower($file));
}
}

Results:

Josh@laptop:~$ find josh
josh
josh/a
josh/b
josh/f1
josh/f2
josh/is
josh/is/awesome
josh/is/awesome/e
josh/is/awesome/t

This code does not take into account uppercase letters in directories but that exercise is up to you.



Related Topics



Leave a reply



Submit