About directory operations in PHP

不言
Release: 2023-04-01 15:34:02
Original
3950 people have browsed it

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:

'; if(is_dir($path.'/'.$file)){ $func = __FUNCTION__; $func($path.'/'.$file,$dep+1); } } }
Copy after login

The running effect is as follows:

'; print_r($res); function showDir($path){ $pos = opendir($path); $next = array(); while(false!==$file=readdir($pos)){ if($file=='.'||$file=='..') continue; $fileinfo = array(); $fileinfo['name'] = $file; if(is_dir($path.'/'.$file)){ $fileinfo['type'] = 'dir'; $func = __FUNCTION__; $fileinfo['next'] = $func($path.'/'.$file); }else{ $fileinfo['type'] = 'file'; } $next[] = $fileinfo; } closedir($pos); return $next; }
Copy after login

The running effect diagram is as follows:

Recursively delete the directory:

'; if(is_dir($path.'/'.$file)){ $func = __FUNCTION__; $func($path.'/'.$file,$dep+1); }else{ unlink($path.'/'.$file); } } rmdir($path); closedir($pos); }
Copy after login

Directory file encoding problem:

Convert operating system encoding to response data encoding when displaying

windows For gbk, the project utf-8

iconv('gbk',utf-8',file);
Copy after login

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

iconv(utf-8','gbk',file);
Copy after login

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!