我想使用PHP将所有图像文件从图像文件夹复制到新文件夹。我尝试了下面的代码,但是有错误。
<?php
$old_dir = 'images/';
$new_dir = 'images/new_update';
$scanned_directory = preg_grep('/^([^.])/', scandir($old_dir));
foreach ($scanned_directory as $key ) {
$source_file = $old_dir.$key;
$destination_path = $new_dir;
if(rename($source_file.'/'.$key,$destination_path.'/'.$key))
echo "Success";
else
echo "Fail";
}
?>
错误:警告:重命名(images / Screenshot 2019-02-04 at 1.24.35 PM.png / Screenshot 2019-02-04 at 1.24.35 PM.png,images / del // Screenshot 2019-02-04在1.24.35 PM.png):在第12行的/Applications/XAMPP/xamppfiles/htdocs/spartanlink/news/gallery/scandir.php中不是目录
我不知道为什么会收到该错误。感谢您的帮助
答案 0 :(得分:0)
您两次引用$key
,一次是定义$source_file
时,一次是调用rename
时。应该更像这样:
$source_file = $old_dir.$key;
$destination_path = $new_dir;
if(rename($source_file,$destination_path.'/'.$key))
echo "Success";