Home > Backend Development > PHP Tutorial > Detailed introduction to directory operations and file operations in PHP_PHP Tutorial

Detailed introduction to directory operations and file operations in PHP_PHP Tutorial

WBOY
Release: 2016-07-20 10:57:50
Original
903 people have browsed it

Directory operations and file operations in PHP have also been introduced, but some people still don’t understand them very well. The following describes PHP's directory operations and file operations in detail.

In any computer device, files are necessary objects, and in Web programming, file operations have always been a headache for web programmers. However, file operations in cms systems are Necessary and very useful. We often encounter operations such as generating file directories and editing files (folders). Now I will give a detailed summary of these functions in PHP and demonstrate how to use them with examples. About correspondence For a detailed introduction to the functions, please refer to the PHP manual. Here we only summarize the key points and points that need attention. (This is not found in the PHP manual.)

1. Directory operations

The first introduction is a function that reads from a directory, opendir(), readdir(), closedir(). When used, the file handle is opened first, and then iteratively listed:

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$base_dir</span><span> = </span><span class="string">"filelist/"</span><span>;   </span></li><li class="alt"><span class="vars">$fso</span><span> = opendir(</span><span class="vars">$base_dir</span><span>);   </span></li><li><span class="func">echo</span><span> </span><span class="vars">$base_dir</span><span>."  </span></li><li class="alt"><span> </span></li><li><span>" ;   </span></li><li class="alt"><span class="keyword">while</span><span>(</span><span class="vars">$flist</span><span>=readdir(</span><span class="vars">$fso</span><span>)){   </span></li><li><span class="func">echo</span><span> </span><span class="vars">$flist</span><span>."   </span></li><li class="alt"><span>" ;   </span></li><li><span>}   </span></li><li class="alt"><span class="func">closedir</span><span>(</span><span class="vars">$fso</span><span>)   </span></li><li><span>?> </span></span></li></ol>
Copy after login

This is a program that returns the files and directories under the file directory (0 files will return false).

Sometimes you need to know the directory information, you can use dirname ($path) and basename ($path), Return the directory part and file name part of the path respectively. You can use disk_free_space($path) to return the free space.

Creation command:

mkdir($path,0777), 0777 is the permission code, you can use the umask() function to set rmdir($path)

in non-windows. The file with the path in $path will be deleted. The

dir -- directory class is also an important class for operating file directories. , there are 3 methods, read, rewind, close. This is a pseudo-object-oriented class. It first uses a file handle to open, and then reads it using a pointer. See the PHP manual here:

<ol class="dp-c">
<li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$d</span><span> = dir(</span><span class="string">"/etc/php5"</span><span>);   </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="string">"Handle: "</span><span> . </span><span class="vars">$d</span><span>->handle . </span><span class="string">"n"</span><span>;   </span></span></li>
<li>
<span class="func">echo</span><span> </span><span class="string">"Path: "</span><span> . </span><span class="vars">$d</span><span>->path . </span><span class="string">"n"</span><span>;   </span>
</li>
<li class="alt">
<span class="keyword">while</span><span> (false !== (</span><span class="vars">$entry</span><span> = </span><span class="vars">$d</span><span>->read())) {   </span>
</li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$entry</span><span>.</span><span class="string">"n"</span><span>;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="vars">$d</span><span>->close();   </span>
</li>
<li class="alt"><span>?> </span></li>
</ol> 
Copy after login

Output:

<ol class="dp-xml">
<li class="alt"><span><span>Handle: Resource id #2   </span></span></li>
<li><span>Path: /etc/php5   </span></li>
<li class="alt"><span>.   </span></li>
<li><span>..   </span></li>
<li class="alt"><span>apache   </span></li>
<li><span>CGI   </span></li>
<li class="alt"><span>cli </span></li>
</ol>
Copy after login

The attributes of the file are also very important. File attributes include creation time, last modification time, owner, file group, type, size, etc.

Below Let’s focus on file operations.

2. File operations

A. Reading files

First, read a file It cannot be read (permission issue), or it does not exist. We can use the is_readable function to obtain the information.

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span></li><li class="alt"><span class="keyword">if</span><span> (</span><span class="func">is_readable</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span></li><li><span class="keyword">die</span><span>(</span><span class="string">'文件不存在或者无法读取'</span><span>);   </span></li><li class="alt"><span>} </span><span class="keyword">else</span><span> {   </span></li><li><span class="func">echo</span><span> </span><span class="string">'存在'</span><span>;   </span></li><li class="alt"><span>}   </span></li><li><span>?> </span></span></li></ol>
Copy after login

The function to determine the existence of a file also includes file_exists (demonstrated below), but this is obviously not as comprehensive as is_readable. When a file exists, you can use

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">"filelist.php"</span><span>;   </span></li><li class="alt"><span class="keyword">if</span><span> (</span><span class="func">file_exists</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span></li><li><span class="keyword">die</span><span>(</span><span class="string">'文件不存在'</span><span>);   </span></li><li class="alt"><span>}   </span></li><li><span class="vars">$data</span><span> = </span><span class="func">file_get_contents</span><span>(</span><span class="vars">$file</span><span>);   </span></li><li class="alt"><span class="func">echo</span><span> htmlentities(</span><span class="vars">$data</span><span>);   </span></li><li><span>?> </span></span></li></ol>
Copy after login

but the file_get_contents function is It is not supported on lower versions. You can create a handle to the file first, and then use a pointer to read all of it:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$fso</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$cacheFile</span><span>, </span><span class="string">'r'</span><span>);   </span></span></li>
<li>
<span class="vars">$data</span><span> = </span><span class="func">fread</span><span>(</span><span class="vars">$fso</span><span>, </span><span class="func">filesize</span><span>(</span><span class="vars">$cacheFile</span><span>));   </span>
</li>
<li class="alt">
<span>fclose(</span><span class="vars">$fso</span><span>); </span>
</li>
</ol>
Copy after login

There is also a way to read binary files:

<ol class="dp-c"><li class="alt"><span><span class="vars">$data</span><span> = implode(</span><span class="string">''</span><span>, file(</span><span class="vars">$file</span><span>)); </span></span></li></ol>
Copy after login

B. Writing files

is the same as reading files. First check if you can write:

<ol class="dp-c"><li class="alt"><span><span> <?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span></li><li class="alt"><span class="keyword">if</span><span> (</span><span class="func">is_writable</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span></li><li><span class="keyword">die</span><span>(</span><span class="string">"我是鸡毛,我不能"</span><span>);   </span></li><li class="alt"><span>}   </span></li><li><span>?> </span></span></li></ol>
Copy after login

If you can write, you can use file_put_contents Function writing

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span></li><li class="alt"><span class="keyword">if</span><span> (</span><span class="func">is_writable</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span></li><li><span class="keyword">die</span><span>(</span><span class="string">'我是鸡毛,我不能'</span><span>);   </span></li><li class="alt"><span>}   </span></li><li><span class="vars">$data</span><span> = </span><span class="string">'我是可鄙,我想要'</span><span>;   </span></li><li class="alt"><span class="func">file_put_contents</span><span> (</span><span class="vars">$file</span><span>, </span><span class="vars">$data</span><span>);   </span></li><li><span>?> </span></span></li></ol>
Copy after login

The file_put_contents function is a newly introduced function in php5 (if you don’t know it exists, use the function_exists function to judge it first). Lower versions of php cannot be used. You can use the following method:

<ol class="dp-c">
<li class="alt"><span><span class="vars">$f</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$file</span><span>, </span><span class="string">'w'</span><span>);   </span></span></li>
<li>
<span>fwrite(</span><span class="vars">$f</span><span>, </span><span class="vars">$data</span><span>);   </span>
</li>
<li class="alt">
<span>fclose(</span><span class="vars">$f</span><span>); </span>
</li>
</ol>
Copy after login

Replace.

Sometimes you need to lock when writing a file, then write:

<ol class="dp-c">
<li class="alt"><span><span class="keyword">function</span><span> cache_page(</span><span class="vars">$pageurl</span><span>,</span><span class="vars">$pagedata</span><span>){   </span></span></li>
<li>
<span class="keyword">if</span><span>(!</span><span class="vars">$fso</span><span>=</span><span class="func">fopen</span><span>(</span><span class="vars">$pageurl</span><span>,</span><span class="string">'w'</span><span>)){   </span>
</li>
<li class="alt">
<span class="vars">$this</span><span>->warns(</span><span class="string">'无法打开缓存文件.'</span><span>);</span><span class="comment">//trigger_error  </span><span> </span>
</li>
<li>
<span class="keyword">return</span><span> false;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="keyword">if</span><span>(!</span><span class="func">flock</span><span>(</span><span class="vars">$fso</span><span>,LOCK_EX)){</span><span class="comment">//LOCK_NB,排它型锁定  </span><span> </span>
</li>
<li class="alt">
<span class="vars">$this</span><span>->warns(</span><span class="string">'无法锁定缓存文件.'</span><span>);</span><span class="comment">//trigger_error  </span><span> </span>
</li>
<li>
<span class="keyword">return</span><span> false;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="keyword">if</span><span>(!fwrite(</span><span class="vars">$fso</span><span>,</span><span class="vars">$pagedata</span><span>)){</span><span class="comment">//写入字节流,serialize写入其他格式  </span><span> </span>
</li>
<li class="alt">
<span class="vars">$this</span><span>->warns(</span><span class="string">'无法写入缓存文件.'</span><span>);</span><span class="comment">//trigger_error  </span><span> </span>
</li>
<li>
<span class="keyword">return</span><span> false;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li>
<span class="func">flock</span><span>(</span><span class="vars">$fso</span><span>,LOCK_UN);</span><span class="comment">//释放锁定  </span><span> </span>
</li>
<li class="alt">
<span>fclose(</span><span class="vars">$fso</span><span>);   </span>
</li>
<li>
<span class="keyword">return</span><span> true;   </span>
</li>
<li class="alt"><span>} </span></li>
</ol>
Copy after login

C. Copy and delete the file

It is very easy to delete files in PHP. Use the unlink function:

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span></li><li class="alt"><span class="vars">$result</span><span> = @unlink (</span><span class="vars">$file</span><span>);   </span></li><li><span class="keyword">if</span><span> (</span><span class="vars">$result</span><span> == false) {   </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="string">'蚊子赶走了'</span><span>;   </span></li><li><span>} </span><span class="keyword">else</span><span> {   </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="string">'无法赶走'</span><span>;   </span></li><li><span>}   </span></li><li class="alt"><span>?> </span></span></li></ol>
Copy after login

.

It is also easy to copy files:

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">'yang.txt'</span><span>;   </span></li><li class="alt"><span class="vars">$newfile</span><span> = </span><span class="string">'ji.txt'</span><span>; # 这个文件父文件夹必须能写   </span></li><li><span class="keyword">if</span><span> (</span><span class="func">file_exists</span><span>(</span><span class="vars">$file</span><span>) == false) {   </span></li><li class="alt"><span class="keyword">die</span><span> (</span><span class="string">'小样没上线,无法复制'</span><span>);   </span></li><li><span>}   </span></li><li class="alt"><span class="vars">$result</span><span> = </span><span class="func">copy</span><span>(</span><span class="vars">$file</span><span>, </span><span class="vars">$newfile</span><span>);   </span></li><li><span class="keyword">if</span><span> (</span><span class="vars">$result</span><span> == false) {   </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="string">'复制记忆ok'</span><span>;   </span></li><li><span>}   </span></li><li class="alt"><span>?> </span></span></li></ol>
Copy after login

You can use rename () function to rename a folder. Other operations can be achieved by combining these functions.

D. Get file attributes

Let me say a few A common function:

Get the latest modification time:

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">'test.txt'</span><span>;   </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="func">date</span><span>(</span><span class="string">'r'</span><span>, </span><span class="func">filemtime</span><span>(</span><span class="vars">$file</span><span>));   </span></li><li><span>?> </span></span></li></ol>
Copy after login

returns the Unix timestamp, which is commonly used in caching technology.

is also related to getting The time when the file was last accessed fileatime(), filectime() When the file's permissions, owner, all groups or other metadata in the inode are updated, the fileowner() function returns the file owner

<ol class="dp-c"><li class="alt"><span><span class="vars">$owner</span><span> = posix_getpwuid(</span><span class="func">fileowner</span><span>(</span><span class="vars">$file</span><span>)); </span></span></li></ol>
Copy after login

(Non-window system), ileperms() gets the permissions of the file,

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span></li><li class="alt"><span class="vars">$perms</span><span> = </span><span class="func">substr</span><span>(sprintf(</span><span class="string">'%o'</span><span>, </span><span class="func">fileperms</span><span>(</span><span class="vars">$file</span><span>)), -4);   </span></li><li><span class="func">echo</span><span> </span><span class="vars">$perms</span><span>;   </span></li><li class="alt"><span>?> </span></span></li></ol>
Copy after login

filesize() returns the number of bytes of the file size:

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="comment">// 输出类似:somefile.txt: 1024 bytes  </span><span> </span></li><li class="alt"><span class="vars">$filename</span><span> = </span><span class="string">'somefile.txt'</span><span>;   </span></li><li><span class="func">echo</span><span> </span><span class="vars">$filename</span><span> . </span><span class="string">': '</span><span> . </span><span class="func">filesize</span><span>(</span><span class="vars">$filename</span><span>) . </span><span class="string">' bytes'</span><span>;   </span></li><li class="alt"><span>?> </span></span></li></ol>
Copy after login

Get all the information of the file: A function that returns an array, the stat() function:

<ol class="dp-c"><li class="alt"><span><span><?php   </span></span></li><li><span class="vars">$file</span><span> = </span><span class="string">'dirlist.php'</span><span>;   </span></li><li class="alt"><span class="vars">$perms</span><span> = stat(</span><span class="vars">$file</span><span>);   </span></li><li><span>var_dump(</span><span class="vars">$perms</span><span>);   </span></li><li class="alt"><span>?> </span></span></li></ol>
Copy after login

You can check the detailed information about which key corresponds to it and will not expand it here.

4. Conclusion

I briefly summarized several file operations above. If you are proficient in the functions listed above, there will be no major problems in the operation. The functions of PHP file operations change relatively quickly and are now very powerful. Yes, this part of the file is also a very important part of learning PHP. I hope you won’t ignore it.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445748.htmlTechArticle Directory operations and file operations in PHP have been introduced before, but some people still don’t understand them very well. The following describes PHP's directory operations and file operations in detail. On any computer device...
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