数组中每个文件名的php复制文件

时间:2016-12-01 13:03:45

标签: php copy

我正在尝试将数组中的所有文件从一个目录移动到另一个目录。

我做了一些研究并使用了php Copy()函数。 这是我目前的代码:

$filenameArray = "img1.png,img2.png,img3.png";

$sourcePath = "/source/";
$savePath = "/newDir/";

$myArray = explode(',', $filenameArray);
$finalArray = print_r($myArray);

function copyFiles($finalArray,$sourcePath,$savePath) {
for($i = 0;$i < count($finalArray);$i++){
    copy($sourcePath.$finalArray[$i],$savePath.$finalArray[$i]);}
} 

任何人都会看到我出错的地方?

提前致谢!

这是我一直试图使用的取消关联。

function copyFiles($finalArray,$sourcePath,$savePath) {
   foreach ($finalArray as $file){
     if (!copy($sourcePath.$file,$savePath.$file)) {
         echo "Failed to move image";
     }

     $delete[] = $sourcePath.$file;

   }

}

   // Delete all successfully-copied files
    foreach ( $delete as $file ) {
        unlink( $sourcePath.$file );
    }

我的最终工作代码

下面的代码将逗号分隔数组中的图像移动到新文件夹,并将其从当前文件夹中删除

$finalArray = explode(',', $filenameArray);

function copyFiles($finalArray,$sourcePath,$savePath) {
   foreach ($finalArray as $file){
     if (!copy($sourcePath.$file,$savePath.$file)) {
         echo "Failed to move image";
     }

   }

}

copyFiles( $finalArray, $sourcePath, $savePath);

function removeFiles($finalArray,$sourcePath) {
   foreach ($finalArray as $file){
     if (!unlink($sourcePath.$file)) {
         echo "Failed to remove image";
     }

   }

}

removeFiles( $finalArray, $sourcePath);

2 个答案:

答案 0 :(得分:1)

在您的代码中,您没有调用copyFile函数。试试这个:

$filenameArray = "img1.png,img2.png,img3.png";

$sourcePath = "/source/";
$savePath = "/newDir/";

$finalArray = explode(',', $filenameArray);

function mvFiles($finalArray,$sourcePath,$savePath) {
   foreach ($finalArray as $file){
     if (!rename($sourcePath.$file,$savePath.$file)) {
         echo "failed to copy $file...\n";
     }
   }
}

mvFiles( $finalArray, $sourcePath, $savePath);

答案 1 :(得分:0)

一个简单的解决方案:

$filenameArray = "img1.png,img2.png,img3.png";

$sourcePath = "/source/";
$savePath = "/newDir/";

$myArray = explode(',', $filenameArray);
$finalArray = $myArray;    //corrected this line

function copyFiles($finalArray, $sourcePath, $savePath) 
{
    for ($i = 0; $i < count($finalArray); $i++) 
    {
        copy($sourcePath.$finalArray[$i],$savePath.$finalArray[$i]);
    }
}

希望您有权致电function copyFiles()

unlink()更新:

让我试着说明你的工作(书面代码):

foreach ($finalArray as $file)
{
    if (!copy($sourcePath.$file,$savePath.$file)) 
    {
        echo "Failed to move image";
    }
    $delete[] = $sourcePath.$file;
}

$ delete的内容:

a. /source/img1.png
b. /source/img2.png
c. /source/img3.png

现在,

foreach ( $delete as $file ) 
{
    unlink( $sourcePath.$file );
}

将使用以下参数调用unlink():

$sourcePath.$file : /source/./source/img1.png : /source//source/img1.png => No  such path exists
$sourcePath.$file : /source/./source/img2.png : /source//source/img2.png => No  such path exists
$sourcePath.$file : /source/./source/img3.png : /source//source/img3.png => No  such path exists
$sourcePath.$file : /source/./source/img4.png : /source//source/img4.png => No  such path exists

我认为出于这个原因,unlink无效。

要编写的代码应如下所示:

foreach ( $delete as $file ) 
{
    unlink( $file );
}

现在,将使用以下参数调用unlink():

a. /source/img1.png => path do exists
b. /source/img2.png => path do exists
c. /source/img3.png => path do exists

请告诉我这是否解决了这个问题。

根据Dave Lynch的代码更新:

$filenameArray = "img1.png,img2.png,img3.png";

$sourcePath = "/source/";
$savePath = "/newDir/";

$finalArray = explode(',', $filenameArray);

foreach ($finalArray as $file)
{
    $delete[] = $sourcePath.$file;
}

foreach ( $delete as $file ) 
{
     echo $sourcePath.$file . "</br>";
}

<强>输出:

/source//source/img1.png
/source//source/img2.png
/source//source/img3.png

请检查。

谢谢和问候,