简体   繁体   English

重命名PHP中的文件?

[英]Renaming files in PHP?

I want the script to go into the folder 'images', take every file, cut the first four characters and rename it. 我希望脚本进入文件夹“ images”,获取每个文件,剪切前四个字符并将其重命名。

PHP 的PHP

<?php
$path = './images/';

if ($handle = opendir($path))
{
    while (false !== ($fileName = readdir($handle)))
    {
        if($fileName!=".." && $fileName!=".")
        {
            $newName = substr($fileName, 4);
            $fileName = $path . $fileName;
            $newName = $path . $newName;

            rename($fileName, $newName);
        }
    }

    closedir($handle);
}
?>

This is how the files in the images folder are named: 这是images文件夹中文件的命名方式:

0,78test-1.jpg
0,32test-2.jpg
0,43test-3.jpg
0,99test-4.jpg

and this is what i want them to look like: 这就是我希望他们看起来像的样子:

test-1.jpg
test-2.jpg
test-3.jpg
test-4.jpg

The problem is the script cuts out the first 8, 12 or 16 characters, not four as i want it! 问题在于脚本会剪切掉前8个,12个或16个字符,而不是我想要的四个! So when i execute it my files look like this: 因此,当我执行它时,我的文件如下所示:

-1.jpg
-2.jpg
-3.jpg
-4.jpg

UPDATE 更新

I also tracked the packages to make sure i am not executing the script multiple times. 我还跟踪了软件包,以确保我没有多次执行脚本。 The script is only executed once! 该脚本仅执行一次!

A slightly different approach though essentially the same with the substr part this worked fine for tests on local system. 稍微不同的方法,虽然本质上与同一substr部分这个工作的罚款在本地系统上测试。

$dir='c:/temp2/tmpimgs/';
$files=glob( $dir . '*.*' );
$files=preg_grep( '@(\.jpg$|\.jpeg$|\.png$)@i', $files );


foreach( $files as $filename ){
    try{

        $path=pathinfo( $filename, PATHINFO_DIRNAME );
        $name=pathinfo( $filename, PATHINFO_BASENAME );
        $newname=$path . DIRECTORY_SEPARATOR . substr( $name, 4, strlen( $name ) );

        if( strlen( $filename ) > 4 ) rename( $filename, $newname );

    } catch( Exception $e ){
        echo $e->getTraceAsString();
    }
}

You may want to try this little Function. 您可能要尝试这个小功能。 It would do just the proper renaming for you: 它将为您进行适当的重命名:

<?php

    $path   = './images/';

    function renameFilesInDir($dir){
        $files   = scandir($dir);

        // LOOP THROUGH THE FILES AND RENAME THEM
        // APPROPRIATELY...
        foreach($files as $key=>$file){
            $fileName   = $dir . DIRECTORY_SEPARATOR . $file;
            if(is_file($fileName) && !preg_match("#^\.#", $file)){
                $newFileName = preg_replace("#\d{1,},\d{1,}#", "", $fileName);
                rename($fileName, $newFileName);
            }
        }
    }

    renameFilesInDir($path);
<?php
$path = './images/';

if ($handle = opendir($path))
{
    while (false !== ($fileName = readdir($handle)))
    {
        if($fileName!=".." && $fileName!=".")
        {

//change below line and find first occurence of '-' and then replace everything before this with 'test' or any keyword
            $newName = substr($fileName, 4);

            $fileName = $path . $fileName;
            $newName = $path . $newName;

            rename($fileName, $newName);
        }
    }

    closedir($handle);
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM