Home > Backend Development > PHP Problem > What is the code to delete files in php

What is the code to delete files in php

coldplay.xixi
Release: 2023-03-03 21:16:02
Original
2985 people have browsed it

php file deletion code: first set parameters to delete the file path, the code is [$path2= "./upload/picture/"]; then use the public method to delete, the code is [public function delDirAndFile($path , $delDir= ].

What is the code to delete files in php

php deletion file code:

Sometimes we need to delete the newly generated temporary Files, such as when uploading pictures or generating pictures, we need to store them locally and then upload them to the picture server. When the pictures are uploaded to the server, the locally stored pictures are useless. In order to avoid the project file being too large, so It becomes necessary to delete local image files.

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();
   }
Copy after login

The above will delete the code part of the folder or file.

Related learning recommendations: php programming (video)

The above is the detailed content of What is the code to delete files in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template