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

数组中每个文件名的php复制文件,php,copy,Php,Copy,我正在尝试将数组中的所有文件从一个目录移动到另一个目录 我做了一些研究,正在使用php Copy()函数。 以下是我目前的代码: $filenameArray = "img1.png,img2.png,img3.png"; $sourcePath = "/source/"; $savePath = "/newDir/"; $myArray = explode(',', $filenameArray); $finalArray = print_r($myArray); function co

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

我做了一些研究,正在使用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]);}
} 
我的最终工作代码

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

$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);
一个简单的解决方案:

$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]);
    }
}
删除$delete的内容:

a. /source/img1.png
b. /source/img2.png
c. /source/img3.png
现在,

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
a. /source/img1.png => path do exists
b. /source/img2.png => path do exists
c. /source/img3.png => path do exists
$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
我认为由于这个原因,取消链接不起作用

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

foreach ( $delete as $file ) 
{
    unlink( $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
a. /source/img1.png => path do exists
b. /source/img2.png => path do exists
c. /source/img3.png => path do exists
$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
如果这不能解决问题,请告诉我

根据Dave Lynch的代码更新:

$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
a. /source/img1.png => path do exists
b. /source/img2.png => path do exists
c. /source/img3.png => path do exists
$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
请查收


感谢并问候,

在您的代码中,您没有调用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);

您有任何错误吗?您在这里面临的问题是什么?@pranavm.s文件没有被移动吗web服务器有权限写入newDir吗?@WilliamMacdonald是的,我曾经使用copy()功能使用相同的directoriesworks一次移动一个文件!只需从/source/中删除文件,谢谢William!您需要使用重命名函数来移动文件。@Dave Lynch:Echo并查看您是否获得正确的源路径和目标路径我得到了williams的建议,但现在我需要一种从源中删除成功移动的图像的方法/i尝试使用unlink()但没有luck@DaveLynch:您是否使用了
unlink()
类似的
unlink(“source/”).$file)
?@DaveLynch:我已经更新了我的答案,并试图解决这个问题。看看是否有帮助。恐怕这也不起作用,我用wrapped创建了第二个foreach循环,并使用一个函数来进行解链接:)