【问题标题】:Copy Image files from old folder to new folder using PHP使用 PHP 将图像文件从旧文件夹复制到新文件夹
【发布时间】:2019-03-10 16:32:40
【问题描述】:

我想使用 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 中的目录

我不知道为什么会出现这个错误。感谢您的帮助

【问题讨论】:

  • 我认为您可以重命名文件夹,创建一个新文件夹并将一个文件夹放入另一个文件夹中?

标签: php


【解决方案1】:

您引用了两次$key,一次是在定义$source_file 时,一次是在调用rename 时。它应该更像这样:

$source_file = $old_dir.$key;
$destination_path = $new_dir;

if(rename($source_file,$destination_path.'/'.$key))
   echo "Success";

【讨论】:

  • 进行了更正,现在得到Warning: rename(images/Screenshot 2019-02-04 at 1.24.35 PM.png,images/del/Screenshot 2019-02-04 at 1.24.35 PM.png): Permission denied in /Applications/XAMPP/xamppfiles/htdocs/spartanlink/news/gallery/scandir.php on line 12 Fail Warning: rename(images/del,images/del/del): Invalid argument in /Applications/XAMPP/xamppfiles/htdocs/spartanlink/news/gallery/scandir.php on line 12 Fail
  • 您需要在源文件夹和目标文件夹中具有读写权限。
  • 在授予权限后为我工作。谢谢
猜你喜欢
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多