2016-12-01 84 views
1

我想將我的數組中的所有文件從一個目錄移動到另一個目錄。php複製陣列中每個文件名的文件

我已經做了一些研究,並使用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); 
+0

你有什麼錯誤嗎? – Tiger

+0

你在這裏面臨的問題是什麼? –

+0

@ pranavm.s文件未被移動 –

回答

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; 
} 

的$內容刪除:

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 

我覺得這個原因,斷開鏈接無法正常工作。

代碼被寫入應該是這樣的:

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

現在,取消鏈接()將以下參數來調用:

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 

請檢查。

感謝和問候,

+0

不幸的是,沒有工作:/ –

+0

@Dave Lynch:回聲,看看你是否得到正確的源和目標路徑 –

+0

我得到了威廉姆斯建議工作,但現在我需要一種方法刪除成功移動圖像從源/ ive嘗試使用unlink()但沒有運氣 –

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); 
+0

https://gist.github.com/willmacdonald/e564c46af857e6d61be2cb5c9166d876 –

+0

工程!只需要從/源/刪除文件感謝威廉! –

+0

您需要使用重命名功能來移動文件。 –