2015-09-18 77 views
0

我試圖將所有文件移動到另一個目錄,但我無法讓它工作。PHP移動目錄中的所有文件

$files = glob($_SERVER['DOCUMENT_ROOT']."/*.csv"); // get all file names 
foreach($files as $file) { 
    if(is_file($file)) 
     rename($file, "archive/".basename($file)); 
    } 
} 

我沒有錯誤。提前致謝。

+0

你得到錯誤信息? –

+1

你看過http://stackoverflow.com/questions/2082138/move-all-files-in-a-folder-to-another? – Joseph

+1

如果您的DOCUMENT_ROOT不以斜槓結尾,請嘗試使用glob($ _ SERVER ['DOCUMENT_ROOT']。「/ *。csv」)'... – bufh

回答

0

好的,我得到它的工作。我沒有$_SERVER['DOCUMENT_ROOT']

$files = glob("files/*.csv"); // get all file names 
foreach($files as $file){ // iterate files 
    if(is_file($file)) { 
     rename($file, "files/archive/" . basename($file)); // move file 
    } 
} 

感謝您的幫助。

0

這是一個很好的做法,調試代碼....

// here you missed the backslash, like it is mentioned by bufh 
$files = glob($_SERVER['DOCUMENT_ROOT']."/*.csv"); 

var_dump($files); // to see if files with path were effectively returned by glob() 

foreach($files as $file) { 
    if(is_file($file)) 
     // i advice you to use Log4PHP ... it let you debug easily your code in development and production system (simple switch to debug mode...) 
     // $log->debug(sprintf("this file:%s will be moved")); 
     rename($file, "archive/".basename($file)); 
    } 
} 
相關問題