Home  >  Article  >  Backend Development  >  About directory operations in PHP

About directory operations in PHP

不言
不言Original
2018-06-21 09:36:174013browse

This article mainly introduces PHP directory operations. It summarizes and analyzes PHP's related functions and usage skills for common operations such as directory reading, traversal, and closing in the form of examples. Friends in need can refer to it

The example in this article summarizes the PHP directory operation method. Share it with everyone for your reference, the details are as follows:

Directory operation

New directory: mkdir (path, permissions, recursive creation)

Delete directory: rmdir()

Move (rename): rename()

Get directory content

//Open directory

Directory handle = opendir()

//Read directory

File name = readdir(directory handle)

Read the file names in sequence and move the file handle pointer downward at the same time. If it cannot be read, return false

//Close the directory

closedir()

Recursively read the directory content:

<?php
showDir(&#39;../../file&#39;);
function showDir($path,$dep=0){
 $pos = opendir($path);
 while(false!==$file=readdir($pos)){
  if($file==&#39;.&#39;||$file==&#39;..&#39;) continue;
  echo str_repeat("&nbsp",$dep*4),$file.&#39;</br>&#39;;
  if(is_dir($path.&#39;/&#39;.$file)){
   $func = __FUNCTION__;
   $func($path.&#39;/&#39;.$file,$dep+1);
  }
 }
}

The running effect is as follows:

<?php
$res = showDir(&#39;../../file&#39;);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($res);
function showDir($path){
 $pos = opendir($path);
 $next = array();
 while(false!==$file=readdir($pos)){
  if($file==&#39;.&#39;||$file==&#39;..&#39;) continue;
  $fileinfo = array();
  $fileinfo[&#39;name&#39;] = $file;
  if(is_dir($path.&#39;/&#39;.$file)){
   $fileinfo[&#39;type&#39;] = &#39;dir&#39;;
   $func = __FUNCTION__;
   $fileinfo[&#39;next&#39;] = $func($path.&#39;/&#39;.$file);
  }else{
   $fileinfo[&#39;type&#39;] = &#39;file&#39;;
  }
  $next[] = $fileinfo;
 }
 closedir($pos);
 return $next;
}

The running effect diagram is as follows:

Recursively delete the directory:

<?php
showDir(&#39;../../file/sim&#39;);
function showDir($path,$dep=0){
 $pos = opendir($path);
 while(false!==$file=readdir($pos)){
  if($file==&#39;.&#39;||$file==&#39;..&#39;) continue;
//  echo str_repeat("&nbsp",$dep*4),$file.&#39;</br>&#39;;
  if(is_dir($path.&#39;/&#39;.$file)){
   $func = __FUNCTION__;
   $func($path.&#39;/&#39;.$file,$dep+1);
  }else{
   unlink($path.&#39;/&#39;.$file);
  }
 }
 rmdir($path);
 closedir($pos);
}

Directory file encoding problem:

Convert operating system encoding to response data encoding when displaying

windows For gbk, the project utf-8

iconv(&#39;gbk&#39;,utf-8&#39;,file);

code address exists in Chinese: it needs to be converted to system encoding

iconv(utf-8&#39;,&#39;gbk&#39;,file);

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the analysis of php_pdo preprocessing statements

About PHP’s linked list operation

The above is the detailed content of About directory operations in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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