Home Backend Development PHP Tutorial Detailed explanation of the functions of PHP file system related functions_PHP tutorial

Detailed explanation of the functions of PHP file system related functions_PHP tutorial

Jul 15, 2016 pm 01:29 PM
php windows about function Function Can exist right us document of Related system explain Detailed explanation path pass

We explain the file path through : In Windows, you can use / and symbols in the path, while in other operating systems only / symbols can be used. For the sake of consistency, we use the / notation uniformly.

The following PHP file system related function script example can display a directory listing, the comments have been included in the code:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> /*把要读取的目录的全路径名存入一个名字为$dir_name的变量中。 */   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>dir_name</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>"/home/me/"</FONT></SPAN><SPAN>;   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>/* 创建一个句柄,其值是打开一个给定目录的结果*/   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>dir</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>opendir</FONT></SPAN><SPAN>($dir_name);   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>/* 建立一个文字块,用以放置列表元素(文件名字)*/   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>file_list</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>"<ul>"</font></strong></span><span>;   </span></span></li>
<li class=""><span> </span></li>
<li class="alt"><span>/* 使用一个while语句,读取已经打开的目录中的所有元素,如果文件的名字不是“.”或“..”,则显示列表中的名字*/   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>while ($</span><span class="attribute"><font color="#ff0000">file_name</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">readdir</font></span><span>($dir)) {   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>if (($file_name != ".") && ($file_name != "..")) {   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$file_list </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<li>$file_name"</font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>}   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$file_list </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"</ul>"</font></span><span>;   </span>
</li>
<li class=""><span> </span></li>
<li class="alt"><span>/*关闭打开的目录,结束PHP模块*/   </span></li>
<li class=""><span> </span></li>
<li class="alt"><span>closedir($dir);   </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="comments"><font color="#008200"><!-- Start your HTML --></font></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></span></font></strong><span>Directory Listing</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>TITLE</SPAN><SPAN class=tag>></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>HEAD</SPAN><SPAN class=tag>></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="comments"><font color="#008200"><!-- Use PHP to print the name of the directory you read --></font></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>P</SPAN><SPAN class=tag>></span></font></strong><span>Files in: </span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> </SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>echo</FONT></STRONG></SPAN><SPAN> "$dir_name"; </SPAN><STRONG><FONT color=#006699><SPAN class=tag>?></font></strong></span><span class="tag"></</SPAN><SPAN class=tag-name>p</SPAN><SPAN class=tag>></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="comments"><font color="#008200"><!-- Use PHP to print the directory listing --></font></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> </SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>echo</FONT></STRONG></SPAN><SPAN> "$file_list"; </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>BODY</SPAN><SPAN class=tag>></span></font></strong><span>   </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>HTML</SPAN><SPAN class=tag>></span></font></strong><span>  </span>
</li>
</ol>
Copy after login

Okay, we’ve got a directory listing. It should be noted that to read the contents of a file (we will explain this later) or directory, the user on the system where PHP is running must have at least read permissions for the file.

The following is an example of PHP file system related functions on how to copy files:

<ol class="dp-xml"><li class="alt"><span><span class="tag"><strong><font color="#006699"><?</FONT></STRONG></SPAN><SPAN> /*把你要拷贝的原文件的全路径赋给一个名字为$original的变量中,把拷贝的文件的全路径赋予一个名字为$copied的变量中*/   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>original</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>"/home/me/mydatabasedump"</FONT></SPAN><SPAN>;   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>copied</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>"/archive/mydatabasedumo_1010"</FONT></SPAN><SPAN>;   </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>/* 使用copy()函数拷贝原始文件,如果拷贝没有完成则会显示一个错误信息*/   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>@copy($original, $copied) or die("Couldn't copy file.");   </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></strong></span><span>  </span></span></li></ol>
Copy after login

This example of PHP file system related functions is a prototype of a file backup system. When this script runs, it copies the file to a different location and saves it. Modify the daemon slightly so that it can be executed at times of day you specify without user intervention.

Assuming you have Lynx installed on your system, you can create a daemon entry to access this file. Accessing this file will run this script and create a copy of the file. The following example will run this at 5 AM Script, then shut down Lynx:

0 5 * * * [username] lynx -dump http://localhost/copyfile.php 1>/dev/null 2>&1

if running It is the CGI version of PHP. You can skip the Lynx part and call the binary file directly:

0 5 * * * [username] php /path/to/copyfile.php 1>/dev/null 2>&1


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446330.htmlTechArticleWe pass the explanation about the file path: In Windows, you can use / and symbols in the path, while in In other operating systems, only the / symbol can be used. For the sake of consistency, we...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

What happens if session_start() is called multiple times? What happens if session_start() is called multiple times? Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How to handle high DPI display in C? How to handle high DPI display in C? Apr 28, 2025 pm 09:57 PM

Handling high DPI display in C can be achieved through the following steps: 1) Understand DPI and scaling, use the operating system API to obtain DPI information and adjust the graphics output; 2) Handle cross-platform compatibility, use cross-platform graphics libraries such as SDL or Qt; 3) Perform performance optimization, improve performance through cache, hardware acceleration, and dynamic adjustment of the details level; 4) Solve common problems, such as blurred text and interface elements are too small, and solve by correctly applying DPI scaling.

Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

H5: Key Improvements in HTML5 H5: Key Improvements in HTML5 Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

See all articles