-1

我正在尝试重命名单个目录中的一些 jpeg。代码一半的工作原理是使用正确的文件名重命名它们,但由于某种原因,新文件名用双引号括起来,这使得它们无法从我的网页访问。

任何帮助表示赞赏!

$i = 10000;

foreach ($imgArray as $v) {

    $html_file_name = basename($v).PHP_EOL;
    $html_file_name =  str_replace(range(0,9),'', $html_file_name);

    $path = pathinfo($v, PATHINFO_DIRNAME);

    $target = ++$i . $html_file_name;

    rename ($v, $path . '/' . $target);

}

好的,这里是 var_dump($imgArray):

array(3) { [0]=> string(47) "../img/gallery/this-is-the-first/10002-vddf.jpg" [1]=> string(51) "../img/gallery/this-is-the-first/10001-vfdssddf.jpg" [2]=> string(50) "../img/gallery/this-is-the-first/10003-vddsvsf.jpg" }

序列化:

a:3:{i:0;s:47:"../img/gallery/this-is-the-first/10002-vddf.jpg";i:1;s:51:"../img/gallery/this-is-the-first/10001-vfdssddf.jpg";i:2;s:50:"../img/gallery/this-is-the-first/10003-vddsvsf.jpg";}
4

1 回答 1

0

您在文件名中有新行问题

  $html_file_name = basename($v).PHP_EOL;
                                    ^-------- Appending End of Line to File Name

所有你需要的是

$i = 10000;
foreach ($imgArray as $v) {
    rename($v, pathinfo($v, PATHINFO_DIRNAME) . '/' . (++$i . str_replace(range(0,9),'',  basename($v))));
}
于 2012-10-17T12:18:05.460 回答