Home > Article > Backend Development > How to delete temporary files in php
php method to delete temporary files: first open the corresponding PHP file; then pass in two parameters in the file, one of which is the path of the file that needs to be deleted; then create a delDirAndFile method to implement the deletion function; Finally save the file.
Recommended: "PHP Tutorial"
php delete folder (temporary file) code
Sometimes we need to delete the newly generated temporary files. For example, when uploading or generating images, we need to store them locally and then upload them to the image server. After the image is uploaded to the server, the locally stored image is useless. In order to avoid the project file being too large, it becomes necessary to delete the local image file.
Share a piece of code directly:
//需要传两个参数,一个是我们需要删除的文件路径,比如: $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(); }
The above will delete the code part of the folder or file. (By the way, the editor’s code color looks really ugly...)
The above is the detailed content of How to delete temporary files in php. For more information, please follow other related articles on the PHP Chinese website!