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