Delete and rename files using unlink() and rename()

Roar Host

I have a folder, which contains thousands of images for my website products, looking like this:

D25200-1.jpg
D25200-2.jpg
D25200-3.jpg
D25201-1.jpg
D25201-2.jpg
D25201-3.jpg
D25201-4.jpg

Which means, that product D25200 has 3 photos and D25201 has 4 photos.

I want to delete some of the images, but keep the count from 1. For example, if I delete D25201-1.jpg and D25201-2, the rest of the images to get renamed as D25201-1.jpg and D25201-2.jpg.

This is the code I've created so far, but it only deletes the photos:

<?php
$codes = array(
    'D25201' => '1,2'
);

$dirs = array(
    dirname(__FILE__) . '/products/',
    dirname(__FILE__) . '/products/large/',
    dirname(__FILE__) . '/products/medium/',
    dirname(__FILE__) . '/products/small/'
);

foreach($codes as $code => $photos) {
    $photos = explode(',', $photos);

    foreach($photos as $photo) {
        foreach($dirs as $dir) {
            unlink($dir . $code . '-' . $photo . '.jpg');
        }
    }
}
?>
Philip G

I havn't had time to test this but something like this should do it (Or atleast give you a hint at a possible direction the figure the rest out yourself):

$codes = array(
    'D25201' => '1,2'
);

$dirs = array(
    dirname(__FILE__) . '/products/',
    dirname(__FILE__) . '/products/large/',
    dirname(__FILE__) . '/products/medium/',
    dirname(__FILE__) . '/products/small/'
);

foreach($codes as $code => $photos) {
    $photos = explode(',', $photos);

    foreach($photos as $photo) {
        foreach($dirs as $dir) {
            if(unlink($dir . $code . '-' . $photo . '.jpg');){
              //if unlink is successfull... loop all images in current dir
                $files = scandir($dir);
                $c = 0;
                foreach($files as $file){
                   $c++;
                   //if the name of the image contains the productcode
                   if(strpos($file,$code) !== false){
                        //rename the file
                        rename ($file , $dir . $code . '-' . $c . '.jpg');
                   }
                }
            }
        }
    }
}

There is probably a better way, using less loops, but something like this should work.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Using rename to rename files and directories

分類Dev

Why does `rename` fail to delete the source file, when `unlink` works

分類Dev

Rename Files using wildcard paths

分類Dev

How To Edit/Add/Delete/Rename Files/Folders In Ubuntu Server Using FileZilla?

分類Dev

Rename files, using text-file as source

分類Dev

How to rename files with count?

分類Dev

Rename multiple files randomly

分類Dev

How to rename files with count?

分類Dev

Efficient way to rename files

分類Dev

Rename files in a folder using Python, using name map from Excel

分類Dev

How does one rename multiple files using python?

分類Dev

Copy and rename files recursively using part of folder name for new name

分類Dev

AWK, SED, REGEX to rename files

分類Dev

Looping two files (Rename and Move)

分類Dev

How to rename the set of files with pattern

分類Dev

Batch rename files to a sequential numbering

分類Dev

How to rename files with different extensions

分類Dev

batch rename using windows powershell

分類Dev

Problem with "rename" using regex (Linux)

分類Dev

Git - what does CONFLICT (rename/delete) mean?

分類Dev

Using rename to rename upper case to lower case and add a character

分類Dev

How to rename files to exclude the datetime stamp?

分類Dev

Rename files to be capitalised but not impact on file extensions

分類Dev

How do I rename the extension for a bunch of files?

分類Dev

How do I rename the extension for a bunch of files?

分類Dev

How do I rename all files to lowercase?

分類Dev

Rename files in install part of homebrew formula

分類Dev

Python - rename files incrementally based on julian day

分類Dev

Bash rename files with duplicated file names

Related 関連記事

ホットタグ

アーカイブ