php刪除文件代碼:首先設置參數(shù)來刪除文件路徑,代碼為【$path2= "./upload/picture/"】;然后使用公共方法刪除,代碼為【public function deldirandfile($path, $deldir= 】。
php刪除文件代碼:
我們有時候需要刪除剛生成的臨時文件,比如上傳圖片或者生成圖片的時候,我們需要現(xiàn)在本地存儲起來,然后再上傳到圖片服務器。當圖片上傳到服務器之后,那本地存儲的圖片就沒用了,為了避免項目文件過大,所以刪除本地的圖片文件的就變得很有必要。
直接分享一段代碼:
//需要傳兩個參數(shù),一個是我們需要刪除的文件路徑,比如: $path2= "./upload/picture/"; $this->deldirandfile($path,true);//這是刪除的方法 public function deldirandfile($path, $deldir = true) { if (is_array($path)) { foreach ($path as $subpath) $this->deldirandfile($subpath, $deldir); } if (is_dir($path)) { $handle = opendir($path); if ($handle) { while (false !== ( $item = readdir($handle) )) { if ($item != "." && $item != "..") is_dir("$path/$item") ? $this->deldirandfile("$path/$item", $deldir) : unlink("$path/$item"); } closedir($handle); if ($deldir) return rmdir($path); } } else { if (file_exists($path)) { return unlink($path); } else { return false; } } clearstatcache(); }以上就刪除文件夾或者文件的代碼部分。
相關學習推薦:php編程(視頻)