Asked  7 Months ago    Answers:  5   Viewed   24 times

I have 1000 images in a Folder, which has SKU# word in all the images. For examples

WV1716BNSKU#.zoom.1.jpg
WV1716BLSKU#.zoom.3.jpg

what i need to do is read all the filenames and rename it to the following

WV1716BN.zoom.1.jpg
WV1716BL.zoom.3.jpg

So remove SKU# from filename, is it possible in PHP to do bulk renaming ?

 Answers

51

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

Wednesday, March 31, 2021
 
SilverHorn
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
10

You missed a dot in the file name:

rename($dir.'/'.$data,'image'.$i.'jpg');

This is why they are not clickable. Use this instead:

rename($dir . '/' . $data, 'image' . $i . '.jpg');
// --------------------------------------- ^
Wednesday, March 31, 2021
 
ioleo
answered 7 Months ago
45

Just add a html A tag inside the LI tag you are outputting. Something along these lines should work:

<?php
function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li class="title">';
            if(is_dir($dir.'/'.$ff)){
                echo $ff;
                listFolderFiles($dir.'/'.$ff);
            }else{
                echo '<a href="'.$dir.'/'.$ff.'">'.$ff.'</a>';
            }
            echo '</li>';
        }
    }
    echo '</ol>';
}


listFolderFiles('test');

?>
Wednesday, March 31, 2021
 
cyber_truite
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :