2013-06-22 209 views
0

我想通過一條命令刪除我的域的public_html目錄中的所有文件。對於這個SO的問題之一有這樣的答案:刪除除運行刪除代碼的文件以外的所有文件

<?php 
array_map('unlink', glob("path/to/temp/*")); 
?> 

我該如何添加例外,也許使用此代碼或別的東西?我不想刪除運行刪除代碼的文件,因爲此文件也將位於public_html文件夾中。

回答

0

使用這樣的事情:

function delete_all($filename) { 
    // files listed in this array will not being deleted 
    $exceptions = array (
     'file1', 
     'file2', ... 
    ); 

    if(!in_array($filename, $exceptions)) { 
     unlink($filename); 
    } 
} 

// !!! attention !!! this will delete all files except those listed above 
// from this folder. Make sure you know what you are doing 
array_map('delete_all', glob("path/to/temp/*")); 
+0

什麼,如果目錄是。的根目錄域?像我的服務器上的public_html文件夾?什麼是'glob'中的正確路徑? – coder101

+0

我猜像是'path/to/public_html/*' – hek2mgl

+0

,但是當public_html是服務器上的域名服務目錄時,我該如何使用它?你建議把它留空還是'/'? – coder101

1

閱讀類的結構,你會得到一些有用的功能

<?php 
// this will call your given callback with the file or folder informations, so you can use your logic to delete/copy/move the file/folder 
// note: this is a recursive function 
$literator = new ipDirLiterator(
    "your/path/to/root/or/anyfolder/", // for getting root path, you can use $_SERVER["DOCUMENT_ROOT"] 
    array(
    "file" => function($file) { // your callback to delete files 
     if (basename($file["pathname"]) !== basename(__FILE__)) { 
     unlink($file["pathname"]); 
     } 
    } 
), 
    true 
); 

class ipDirLiterator { 
    protected $basepath = false; 
    protected $callbacks = false; 

    protected $checked = array(); 

    public function __construct($basepath = false, $callbacks = false, $init = false) { 
    $this->basepath = ($basepath) ? realpath($basepath) : false; 
    $this->callbacks = $callbacks; 
    if ($init) { 
     $this->literate(); 
    } 
    } 

    public function literate($dir = false) { 
    if (!$this->basepath) { 
     return false; 
    } 
    if ($dir === $this->basepath) { 
     return false; 
    } 
    if (!$dir) { 
     $dir = $this->basepath; 
    } 

    $dir = realpath($dir); 

    if (strstr($dir, basename($this->basepath)) === false) { 
     return false; 
    } 
    if (in_array($dir, $this->checked)) { 
     return false; 
    } 

    $this->checked[] = $dir; 
    $items = new DirectoryIterator($dir); 

    foreach($items as $item) { 
     if ($item->isDot()) { 
     if ($item->getFilename() === "..") { 
      $this->literate(dirname($item->getPath())); 
     } 
     $this->callback("dot", $this->info($item)); 
     continue; 
     } 
     if ($item->isFile() || $item->isLink()) { 
     $this->callback("file", $this->info($item)); 
     } 
     if ($item->isDir() && !$item->isLink()) { 
     $this->literate($item->getPathname()); 
     $this->callback("dir", $this->info($item)); 
     } 
    } 
    } 

    private function info($item) { 
    $info = array(
     "filename" => $item->getFilename(), 
     "extension" => pathinfo($item->getFilename(), PATHINFO_EXTENSION), 
     "pathname" => $item->getPathname(), 
     "path" => $item->getPath(), 
     "readable" => (bool)$item->isReadable(), 
     "writable" => (bool)$item->isWritable(), 
     "executable" => (bool)$item->isExecutable(), 
     "created_on" => $item->getCTime(), 
     "last access" => $item->getATime(), 
     "modified_on" => $item->getMTime(), 
     "inode" => $item->getInode(), 
     "permissions" => $item->getPerms(), 
     "is_dir" => (bool)$item->isDir(), 
     "is_dot" => (bool)$item->isDot(), 
     "type" => $item->getType(), 
     "group" => $item->getGroup(), 
     "owner" => $item->getOwner(), 
     "size" => $item->getSize() 
    ); 
    return $info; 
    } 

    private function callback($callback = "file", $args = null) { 
    if ($this->callbacks) { 
     if (isset($this->callbacks[$callback])) { 
     call_user_func($this->callbacks[$callback], $args); 
     } 
    } 
    } 
} 
?> 

詳細例如:

<?php 
/** 
* It will recursively go through the all directories and files under the directory you have given. 
* Here we are going to delete all the files order than 1 hour 
**/ 
/** 
* Path to the folder you want to process 
**/ 
$basepath = "any/path/to/files/or/folder/"; 
/** 
* Callbacks 
**/ 
$file_callback = function($file /* information of current file in the loop */) { // your callback for files 
    $modified_time = filemtime($file["pathname"]); 
    $current_time = time(); 
    $time_differnce = ($current_time - $modified_time); 
    if ($time_differnce > 3600) { 
    unlink($file["pathname"]); 
    } 
}; 
$folder_callback = function($file /* information of current folder in the loop */) { // your callback for folders 
    // proceess your folder here 
}; 
/** 
* Initialize the class 
**/ 
$literator = new ipDirLiterator($basepath, array("file" => $file_callback, "dir" => $folder_callback)); 
$literator->literate(); 
?> 
+0

你能提供更多的文字與你的代碼? – tacaswell

+0

詳細示例添加:) – bystwn22

相關問題