2012-11-05 26 views
0

我/的public_html /目錄下有這些文件:PHP移動文件的具體格式VS移動所有文件

0832.php 
1481.php 
2853.php 
3471.php 
index.php 

,我想所有這些XXXX.php(總是以4位數字格式)移動到目錄/ tmp /,但index.php除外。如何用reg-ex和loop做到這一點?

或者,如何將所有文件(包括index.php)首先移動到/ tmp /然後稍後將index.php放回到/ public_html /,您認爲哪一個CPU消耗更少?

最後一件事,我發現這個教程中使用PHP來移動文件:http://www.kavoir.com/2009/04/php-copying-renaming-and-moving-a-file.html

但如何將所有目錄中的文件?

+0

你是什麼BASH問題是什麼意思?我不明白... –

+0

你只有使用PHP才能移動它嗎?如果那是一次,你可以使用shell,比如bash。 – anishsane

+0

由於我的代碼是PHP,我確定我想使用PHP來移動它。 –

回答

1

最好的辦法是直接通過文件系統做到這一點,但如果你非得做它PHP,像這樣的應該做你想做的事情 - 顯然你必須改變路徑,以便它們是正確的。請注意,這假定public_html目錄中可能有其他文件,因此它只能得到4個數字的文件名。

$d = dir("public_html"); 

while (false !== ($entry = $d->read())) { 
    if($entry == '.' || $entry == '..') continue; 
    if(preg_match("@^\d{4}[email protected]", basename($entry, ".php")) { 
     // move the file 
     rename("public_html/".$entry, "/tmp/".$entry)); 
    } 
} 

$d->close(); 
+0

兄弟,您的Reg-Ex在這裏:@^\ d {4} $ @僅用於驗證4位數字,對嗎?但我只是意識到這一點 –

1

的正則表達式其實矯枉過正這一點,因爲我們只需要做一些簡單的字符串匹配:對於這個問題

$dir = 'the_directory/'; 

$handle = opendir($dir) or die("Problem opening the directory"); 

while ($filename = readdir($handle) !== false) 
{ 
    //if ($filename != 'index.php' && substr($filename, -3) == '.php') 
    // I originally thought you only wanted to move php files, but upon 
    // rereading I think it's not what you really want 
    // If you don't want to move non-php files, use the line above, 
    // otherwise the line below 
    if ($filename != 'index.php') 
    { 
     rename($dir . $filename, '/tmp/' . $filename); 
    } 
} 

然後:

alternatively, how about moving all files (including index.php) first to /tmp/ then later on put only index.php back to /public_html/, which one you think is less CPU consuming?

這是可以做到,它會可能在你的CPU上稍微容易一些。然而,這並不重要的原因有幾個。首先,你已經以非常低效的方式通過PHP來實現這個目標,所以你不應該在這個時候考慮這會給你的CPU帶來的壓力,除非你願意在PHP之外這麼做。其次,這會導致更多的磁盤訪問(特別是如果源目錄和目標目錄不在同一磁盤或分區中),並且磁盤訪問比CPU慢得多。

+0

得到這個錯誤:致命錯誤:調用未定義的函數open_dir()在/home/xxxx/public_html/test.php在第4行。文件需要移動實際存儲在public_html/web /中,我試圖使用$ dir = 「網絡」;和$ dir ='/ web'或$ dir ='/ web /'。所有這些都會產生相同的錯誤。我的PHP不支持open_dir函數嗎? –

+1

@羅伯特漢森:對不起,我的錯。它是'opendir',而不是'open_dir' – Jasper

1

其實 - 我去readdir manual頁和拳頭評論讀的是:

loop through folders and sub folders with option to remove specific files. 

<?php 
function listFolderFiles($dir,$exclude){ 
    $ffs = scandir($dir); 
    echo '<ul class="ulli">'; 
    foreach($ffs as $ff){ 
     if(is_array($exclude) and !in_array($ff,$exclude)){ 
      if($ff != '.' && $ff != '..'){ 
      if(!is_dir($dir.'/'.$ff)){ 
      echo '<li><a href="edit_page.php?path='.ltrim($dir.'/'.$ff,'./').'">'.$ff.'</a>'; 
      } else { 
      echo '<li>'.$ff;  
      } 
      if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff,$exclude); 
      echo '</li>'; 
      } 
     } 
    } 
    echo '</ul>'; 
} 

listFolderFiles('.',array('index.php','edit_page.php')); 
?> 
2

您可以使用FilesystemIteratorRegexIterator

$source = "FULL PATH TO public_html"; 
$destination = "FULL PATH TO public_html/tmp"; 

$di = new FilesystemIterator($source, FilesystemIterator::SKIP_DOTS); 
$regex = new RegexIterator($di, '/\d{4}\.php$/i'); 

foreach ($regex as $file) { 
    rename($file, $destination . DIRECTORY_SEPARATOR . $file->getFileName()); 
}