Home > Backend Development > PHP Tutorial > Summary of basic file and directory operations commonly used in PHP, _PHP tutorial

Summary of basic file and directory operations commonly used in PHP, _PHP tutorial

WBOY
Release: 2016-07-13 10:18:11
Original
922 people have browsed it

Summary of basic file and directory operations commonly used in PHP,

Source: http://www.ido321.com/835.html

1. Basic file operations

The basic operations of files include: file judgment, directory judgment, file size, read and write judgment, existence judgment and file time, etc.

<span id="lnum1">   1:</span><span> <?php</span>
Copy after login
<span id="lnum2">   2:</span><span>     header(<span>"content-type"</span>,<span>"text/html;charset=utf-8"</span>);</span>
Copy after login
<span id="lnum3">   3:</span><span> <span>/*</span></span>
Copy after login
<span id="lnum4">   4:</span><span> <span>*声明一个函数,传入文件名获取文件属性</span></span>
Copy after login
<span id="lnum5">   5:</span><span> <span>*@param string $fileName 文件名称</span></span>
Copy after login
<span id="lnum6">   6:</span><span> <span>*/</span></span>
Copy after login
<span id="lnum7">   7:</span><span> <span>function</span> getFilePro($fileName)</span>
Copy after login
<span id="lnum8">   8:</span><span> {</span>
Copy after login
Copy after login
<span id="lnum9">   9:</span><span>     <span>if</span>(!file_exists($fileName))</span>
Copy after login
<span id="lnum10">  10:</span><span>     {</span>
Copy after login
<span id="lnum11">  11:</span><span>         <span>echo</span> <span>'文件不存在<br/>'</span>;</span>
Copy after login
<span id="lnum12">  12:</span><span>         <span>return</span>;</span>
Copy after login
<span id="lnum13">  13:</span><span>     }</span>
Copy after login
<span id="lnum14">  14:</span><span>     <span>/*是否是普通文件*/</span></span>
Copy after login
<span id="lnum15">  15:</span><span>     <span>if</span>(is_file($fileName))</span>
Copy after login
<span id="lnum16">  16:</span><span>     {</span>
Copy after login
Copy after login
<span id="lnum17">  17:</span><span>         <span>echo</span> $fileName.<span>'是一个文件<br/>'</span>;</span>
Copy after login
<span id="lnum18">  18:</span><span>     }</span>
Copy after login
<span id="lnum19">  19:</span><span>     <span>/*是否是目录*/</span></span>
Copy after login
<span id="lnum20">  20:</span><span>     <span>if</span>(is_dir($fileName))</span>
Copy after login
<span id="lnum21">  21:</span><span>     {</span>
Copy after login
<span id="lnum22">  22:</span><span>         <span>echo</span> $fileName.<span>'是一个目录'</span>;</span>
Copy after login
<span id="lnum23">  23:</span><span>     }</span>
Copy after login
<span id="lnum24">  24:</span><span>     <span>/*输出文件的型态*/</span></span>
Copy after login
<span id="lnum25">  25:</span><span>     <span>echo</span> <span>'文件型态是:'</span>.getFileType($fileName).<span>'<br/>'</span>;</span>
Copy after login
<span id="lnum26">  26:</span><span>     <span>/*文件大小,转换单位*/</span></span>
Copy after login
<span id="lnum27">  27:</span><span>     <span>echo</span> <span>'文件大小是:'</span>.getFileSize(filesize($fileName)).<span>'<br/>'</span>;</span>
Copy after login
<span id="lnum28">  28:</span><span>     <span>/*文件是否可读*/</span></span>
Copy after login
<span id="lnum29">  29:</span><span>     <span>if</span>(is_readable($fileName))</span>
Copy after login
<span id="lnum30">  30:</span><span>     {</span>
Copy after login
<span id="lnum31">  31:</span><span>         <span>echo</span> <span>'文件可读<br/>'</span>;</span>
Copy after login
<span id="lnum32">  32:</span><span>     }</span>
Copy after login
Copy after login
<span id="lnum33">  33:</span><span>     <span>/*文件是否可写*/</span></span>
Copy after login
<span id="lnum34">  34:</span><span>     <span>if</span>(is_writable($fileName))</span>
Copy after login
<span id="lnum35">  35:</span><span>     {</span>
Copy after login
<span id="lnum36">  36:</span><span>         <span>echo</span> <span>'文件可写<br/>'</span>;</span>
Copy after login
<span id="lnum37">  37:</span><span>     }</span>
Copy after login
<span id="lnum38">  38:</span><span>     <span>/*文件是否可执行*/</span></span>
Copy after login
<span id="lnum39">  39:</span><span>     <span>if</span>(is_executable($fileName))</span>
Copy after login
<span id="lnum40">  40:</span><span>     {</span>
Copy after login
<span id="lnum41">  41:</span><span>         <span>echo</span> <span>'文件可执行<br/>'</span>;</span>
Copy after login
<span id="lnum42">  42:</span><span>     }</span>
Copy after login
<span id="lnum43">  43:</span>
Copy after login
<span id="lnum44">  44:</span><span>     <span>echo</span> <span>'文件创立时间:'</span>.date(<span>'Y年m月j日'</span>,filectime($fileName)).<span>'<br/>'</span>;</span>
Copy after login
<span id="lnum45">  45:</span><span>     <span>echo</span> <span>'文件最后修改时间:'</span>.date(<span>'Y年m月j日'</span>,filemtime($fileName)).<span>'<br/>'</span>;</span>
Copy after login
<span id="lnum46">  46:</span><span>     <span>echo</span> <span>'文件最后打开时间:'</span>.date(<span>'Y年m月j日'</span>,fileatime($fileName)).<span>'<br/>'</span>;</span>
Copy after login
<span id="lnum47">  47:</span><span> }</span>
Copy after login
<span id="lnum48">  48:</span>
Copy after login
<span id="lnum49">  49:</span><span> <span>/*</span></span>
Copy after login
<span id="lnum50">  50:</span><span> <span>*声明一个函数,返回文件类型</span></span>
Copy after login
<span id="lnum51">  51:</span><span> <span>*@param string $fileName 文件名称</span></span>
Copy after login
<span id="lnum52">  52:</span><span> <span>*/</span></span>
Copy after login
<span id="lnum53">  53:</span><span> <span>function</span> getFileType($fileName)</span>
Copy after login
<span id="lnum54">  54:</span><span> {</span>
Copy after login
<span id="lnum55">  55:</span><span>     $type = <span>''</span>;</span>
Copy after login
<span id="lnum56">  56:</span><span>     <span>switch</span>(filetype($fileName))</span>
Copy after login
<span id="lnum57">  57:</span><span>     {</span>
Copy after login
<span id="lnum58">  58:</span><span>         <span>case</span> <span>'file'</span>:$type .= <span>'普通文件'</span>;<span>break</span>;</span>
Copy after login
<span id="lnum59">  59:</span><span>         <span>case</span> <span>'dir'</span>:$type .= <span>'目录文件'</span>;<span>break</span>;</span>
Copy after login
<span id="lnum60">  60:</span><span>         <span>case</span> <span>'block'</span>:$type .= <span>'块设备文件'</span>;<span>break</span>;</span>
Copy after login
<span id="lnum61">  61:</span><span>         <span>case</span> <span>'char'</span>:$type .= <span>'字符设备文件'</span>;<span>break</span>;</span>
Copy after login
<span id="lnum62">  62:</span><span>         <span>case</span> <span>'filo'</span>:$type .= <span>'管道文件'</span>;<span>break</span>;</span>
Copy after login
<span id="lnum63">  63:</span><span>         <span>case</span> <span>'link'</span>:$type .= <span>'符号链接'</span>;<span>break</span>;</span>
Copy after login
<span id="lnum64">  64:</span><span>         <span>case</span> <span>'unknown'</span>:$type .= <span>'未知文件'</span>;<span>break</span>;</span>
Copy after login
<span id="lnum65">  65:</span><span>         <span>default</span>:</span>
Copy after login
<span id="lnum66">  66:</span><span>     }</span>
Copy after login
<span id="lnum67">  67:</span><span>     <span>return</span> $type;</span>
Copy after login
<span id="lnum68">  68:</span><span> }</span>
Copy after login
<span id="lnum69">  69:</span>
Copy after login
<span id="lnum70">  70:</span><span> <span>/*</span></span>
Copy after login
<span id="lnum71">  71:</span><span> <span>*声明一个函数,返回文件大小</span></span>
Copy after login
<span id="lnum72">  72:</span><span> <span>*@param int $bytes 文件字节数</span></span>
Copy after login
<span id="lnum73">  73:</span><span> <span>*/</span></span>
Copy after login
<span id="lnum74">  74:</span><span> <span>function</span> getFileSize($bytes)</span>
Copy after login
<span id="lnum75">  75:</span><span> {</span>
Copy after login
<span id="lnum76">  76:</span><span>     <span>if</span>($bytes >= pow(2,40))</span>
Copy after login
<span id="lnum77">  77:</span><span>     {</span>
Copy after login
<span id="lnum78">  78:</span><span>         $<span>return</span> = round($bytes / pow(1024,4),2);</span>
Copy after login
<span id="lnum79">  79:</span><span>         $suffix = <span>'TB'</span>;</span>
Copy after login
<span id="lnum80">  80:</span><span>     }</span>
Copy after login
<span id="lnum81">  81:</span><span>     <span>else</span> <span>if</span>($bytes >= pow(2,30))</span>
Copy after login
<span id="lnum82">  82:</span><span>     {</span>
Copy after login
<span id="lnum83">  83:</span><span>         $<span>return</span> = round($bytes / pow(1024,3),2);</span>
Copy after login
<span id="lnum84">  84:</span><span>         $suffix = <span>'GB'</span>;</span>
Copy after login
<span id="lnum85">  85:</span><span>     }</span>
Copy after login
<span id="lnum86">  86:</span><span>     <span>else</span> <span>if</span>($bytes >= pow(2,20))</span>
Copy after login
<span id="lnum87">  87:</span><span>     {</span>
Copy after login
<span id="lnum88">  88:</span><span>         $<span>return</span> = round($bytes / pow(1024,2),2);</span>
Copy after login
<span id="lnum89">  89:</span><span>         $suffix = <span>'MB'</span>;</span>
Copy after login
<span id="lnum90">  90:</span><span>     }</span>
Copy after login
<span id="lnum91">  91:</span><span>     <span>else</span> <span>if</span>($bytes >= pow(2,10))</span>
Copy after login
<span id="lnum92">  92:</span><span>     {</span>
Copy after login
<span id="lnum93">  93:</span><span>         $<span>return</span> = round($bytes / pow(1024,1),2);</span>
Copy after login
<span id="lnum94">  94:</span><span>         $suffix = <span>'KB'</span>;</span>
Copy after login
<span id="lnum95">  95:</span><span>     }</span>
Copy after login
<span id="lnum96">  96:</span><span>     <span>else</span></span>
Copy after login
<span id="lnum97">  97:</span><span>     {</span>
Copy after login
<span id="lnum98">  98:</span><span>         $<span>return</span> = $bytes;</span>
Copy after login
<span id="lnum99">  99:</span><span>         $suffix = <span>'B'</span>;</span>
Copy after login
<span id="lnum100"> 100:</span><span>     }</span>
Copy after login
<span id="lnum101"> 101:</span><span>     <span>return</span> $<span>return</span>.<span>" "</span>.$suffix;</span>
Copy after login
<span id="lnum102"> 102:</span><span> }</span>
Copy after login
<span id="lnum103"> 103:</span>
Copy after login
<span id="lnum104"> 104:</span><span> <span>/*调用函数,传入test目录下的test.txt文件*/</span></span>
Copy after login
<span id="lnum105"> 105:</span><span> getFilePro(<span>'./test/div.html'</span>);</span>
Copy after login
<span id="lnum106"> 106:</span><span> ?></span>
Copy after login
Result:


2. Directory operations

Directory operations include: directory traversal, deletion, copy, size statistics, etc.

1. Traverse the directory

<span id="lnum1">   1:</span><span> <span>/*</span></span>
Copy after login
Copy after login
Copy after login
Copy after login
<span id="lnum2">   2:</span><span> <span>*遍历目录</span></span>
Copy after login
<span id="lnum3">   3:</span><span> <span>*@param string $dirName 目录名</span></span>
Copy after login
Copy after login
Copy after login
<span id="lnum4">   4:</span><span> <span>*/</span></span>
Copy after login
Copy after login
<span id="lnum5">   5:</span><span> <span>function</span> findDir($dirName)</span>
Copy after login
<span id="lnum6">   6:</span><span> {</span>
Copy after login
Copy after login
<span id="lnum7">   7:</span><span>     $num  = 0;  <span>/*统计子文件个数*/</span></span>
Copy after login
<span id="lnum8">   8:</span><span>     $dir_handle = opendir($dirName);  <span>/*打开目录*/</span></span>
Copy after login
<span id="lnum9">   9:</span><span>     <span>/*输出目录文件*/</span></span>
Copy after login
<span id="lnum10">  10:</span><span>     <span>echo</span> <span>'<table border="0" align="center" width="600" cellspacing="0" cellpadding="0">'</span>;</span>
Copy after login
<span id="lnum11">  11:</span><span>     <span>echo</span> <span>'<caption><h2>目录'</span>.$dirName.<span>'下的文件</h2></caption>'</span>;</span>
Copy after login
<span id="lnum12">  12:</span><span>     <span>echo</span> <span>'<tr align="left" bgcolor="#cccccc"'</span>;</span>
Copy after login
<span id="lnum13">  13:</span><span>     <span>echo</span> <span>'<th>文件名</th><th>文件大小</th><th>文件类型</th><th>修改时间</th></tr>'</span>;</span>
Copy after login
<span id="lnum14">  14:</span>
Copy after login
<span id="lnum15">  15:</span><span>     <span>while</span>($file = readdir($dir_handle))</span>
Copy after login
<span id="lnum16">  16:</span><span>     {</span>
Copy after login
Copy after login
<span id="lnum17">  17:</span><span>         $dirFile = $dirName.<span>'/'</span>.$file;</span>
Copy after login
<span id="lnum18">  18:</span><span>         $bgcolor = $num++%2==0?<span>'#ffffff'</span>:<span>'#cccccc'</span>;</span>
Copy after login
<span id="lnum19">  19:</span><span>         <span>echo</span> <span>'<tr bgcolor='</span>.$bgcolor.<span>'>'</span>;</span>
Copy after login
<span id="lnum20">  20:</span><span>         <span>echo</span> <span>'<td>'</span>.$file.<span>'</td>'</span>;</span>
Copy after login
<span id="lnum21">  21:</span><span>         <span>echo</span> <span>'<td>'</span>.filesize($dirFile).<span>'</td>'</span>;</span>
Copy after login
<span id="lnum22">  22:</span><span>         <span>echo</span> <span>'<td>'</span>.filetype($dirFile).<span>'</td>'</span>;</span>
Copy after login
<span id="lnum23">  23:</span><span>         <span>echo</span> <span>'<td>'</span>.date(<span>'Y/n/t'</span>,filemtime($dirFile)).<span>'</td>'</span>;</span>
Copy after login
<span id="lnum24">  24:</span><span>         <span>echo</span> <span>'</tr>'</span>;</span>
Copy after login
<span id="lnum25">  25:</span><span>     }</span>
Copy after login
<span id="lnum26">  26:</span><span>     <span>echo</span> <span>"</table>"</span>;</span>
Copy after login
<span id="lnum27">  27:</span><span>     closedir($dir_handle);  <span>/*关闭目录*/</span></span>
Copy after login
<span id="lnum28">  28:</span><span>     <span>echo</span> <span>"在<b>"</span>.$dirName.<span>"</b>目录下共有<b>"</span>.$num.<span>'</b>个子文件'</span>;</span>
Copy after login
<span id="lnum29">  29:</span><span> }</span>
Copy after login
<span id="lnum30">  30:</span><span> <span>/*传递当前目录下的test目录*/</span></span>
Copy after login
<span id="lnum31">  31:</span><span> findDir(<span>'./test'</span>);</span>
Copy after login

Results



2. Statistical directory size

<span id="lnum1">   1:</span><span> <span>/*</span></span>
Copy after login
Copy after login
Copy after login
Copy after login
<span id="lnum2">   2:</span><span> <span>*统计目录大小</span></span>
Copy after login
<span id="lnum3">   3:</span><span> <span>*@param string $dirName 目录名</span></span>
Copy after login
Copy after login
Copy after login
<span id="lnum4">   4:</span><span> <span>*@return double</span></span>
Copy after login
<span id="lnum5">   5:</span><span> <span>*/</span></span>
Copy after login
Copy after login
<span id="lnum6">   6:</span>
Copy after login
<span id="lnum7">   7:</span><span> <span>function</span> dirSize($dirName)</span>
Copy after login
<span id="lnum8">   8:</span><span> {</span>
Copy after login
Copy after login
<span id="lnum9">   9:</span><span>     $dir_size = 0;</span>
Copy after login
<span id="lnum10">  10:</span><span>     <span>if</span>($dir_handle = @opendir($dirName))</span>
Copy after login
<span id="lnum11">  11:</span><span>     {</span>
Copy after login
<span id="lnum12">  12:</span><span>         <span>while</span> ($fileName = readdir($dir_handle))</span>
Copy after login
<span id="lnum13">  13:</span><span>         {</span>
Copy after login
<span id="lnum14">  14:</span>              /*排除两个特殊目录*/
Copy after login
<span id="lnum15">  15:</span><span>             <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
Copy after login
<span id="lnum16">  16:</span><span>             {</span>
Copy after login
<span id="lnum17">  17:</span><span>                 $subFile = $dirName.<span>'/'</span>.$fileName;</span>
Copy after login
<span id="lnum18">  18:</span><span>                 <span>if</span>(is_file($subFile))</span>
Copy after login
<span id="lnum19">  19:</span><span>                 {</span>
Copy after login
<span id="lnum20">  20:</span><span>                     $dir_size += filesize($subFile);</span>
Copy after login
<span id="lnum21">  21:</span><span>                 }</span>
Copy after login
<span id="lnum22">  22:</span><span>                 <span>if</span>(is_dir($subFile))</span>
Copy after login
<span id="lnum23">  23:</span><span>                 {</span>
Copy after login
<span id="lnum24">  24:</span><span>                     $dir_size += dirSize($subFile);</span>
Copy after login
<span id="lnum25">  25:</span><span>                 }</span>
Copy after login
<span id="lnum26">  26:</span><span>             }</span>
Copy after login
<span id="lnum27">  27:</span><span>         }</span>
Copy after login
<span id="lnum28">  28:</span><span>         closedir($dir_handle);</span>
Copy after login
<span id="lnum29">  29:</span><span>         <span>return</span> $dir_size;</span>
Copy after login
<span id="lnum30">  30:</span><span>     }</span>
Copy after login
<span id="lnum31">  31:</span><span> }</span>
Copy after login
<span id="lnum32">  32:</span><span> <span>/*传递当前目录下的test目录*/</span></span>
Copy after login
<span id="lnum33">  33:</span><span> $dir_size = dirSize(<span>'./test'</span>);</span>
Copy after login
<span id="lnum34">  34:</span><span> <span>echo</span> <span>'./test目录文件大小是:'</span>.round($dir_size / pow(1024,1),2).<span>'KB'</span>;</span>
Copy after login

Results



3. Delete directory

<span id="lnum1">   1:</span><span> <span>/*</span></span>
Copy after login
Copy after login
Copy after login
Copy after login
<span id="lnum2">   2:</span><span> <span>*删除目录</span></span>
Copy after login
<span id="lnum3">   3:</span><span> <span>*@param string $dirName 目录名</span></span>
Copy after login
Copy after login
Copy after login
<span id="lnum4">   4:</span><span> <span>*/</span></span>
Copy after login
Copy after login
<span id="lnum5">   5:</span><span> <span>function</span> delDir($dirName)</span>
Copy after login
<span id="lnum6">   6:</span><span> {</span>
Copy after login
Copy after login
<span id="lnum7">   7:</span><span>     <span>/*php中的mkdir函数就可以创建目录*/</span></span>
Copy after login
<span id="lnum8">   8:</span><span>     <span>if</span>(file_exists($dirName))</span>
Copy after login
<span id="lnum9">   9:</span><span>     {</span>
Copy after login
Copy after login
<span id="lnum10">  10:</span><span>         <span>if</span>($dir_handle = @opendir($dirName))</span>
Copy after login
<span id="lnum11">  11:</span><span>         {</span>
Copy after login
<span id="lnum12">  12:</span><span>             <span>while</span> ($fileName = readdir($dir_handle))</span>
Copy after login
<span id="lnum13">  13:</span><span>             {</span>
Copy after login
<span id="lnum14">  14:</span>                  /*排除两个特殊目录*/
Copy after login
<span id="lnum15">  15:</span><span>                 <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
Copy after login
<span id="lnum16">  16:</span><span>                 {</span>
Copy after login
<span id="lnum17">  17:</span><span>                     $subFile = $dirName.<span>'/'</span>.$fileName;</span>
Copy after login
<span id="lnum18">  18:</span><span>                     <span>if</span>(is_file($subFile))</span>
Copy after login
<span id="lnum19">  19:</span><span>                     {</span>
Copy after login
<span id="lnum20">  20:</span><span>                         unlink($subFile);</span>
Copy after login
<span id="lnum21">  21:</span><span>                     }</span>
Copy after login
<span id="lnum22">  22:</span><span>                     <span>if</span>(is_dir($subFile))</span>
Copy after login
<span id="lnum23">  23:</span><span>                     {</span>
Copy after login
<span id="lnum24">  24:</span><span>                         delDir($subFile);</span>
Copy after login
<span id="lnum25">  25:</span><span>                     }</span>
Copy after login
<span id="lnum26">  26:</span><span>                 }</span>
Copy after login
<span id="lnum27">  27:</span><span>             }</span>
Copy after login
<span id="lnum28">  28:</span><span>             closedir($dir_handle);</span>
Copy after login
<span id="lnum29">  29:</span><span>             rmdir($dirName);</span>
Copy after login
<span id="lnum30">  30:</span><span>             <span>return</span> $dirName.<span>'目录已经删除'</span>;</span>
Copy after login
<span id="lnum31">  31:</span><span>         }</span>
Copy after login
<span id="lnum32">  32:</span><span>     }</span>
Copy after login
Copy after login
<span id="lnum33">  33:</span><span> }</span>
Copy after login
<span id="lnum34">  34:</span><span> <span>/*传递test目录的副本test1*/</span></span>
Copy after login
<span id="lnum35">  35:</span><span> <span>echo</span> delDir(<span>'./test1'</span>);</span>
Copy after login

Prompt message for successful deletion



4. Copy directory

<span id="lnum1">   1:</span><span> <span>/*</span></span>
Copy after login
Copy after login
Copy after login
Copy after login
<span id="lnum2">   2:</span><span> <span>*复制目录</span></span>
Copy after login
<span id="lnum3">   3:</span><span> <span>*@param string $dirSrc 原目录名</span></span>
Copy after login
<span id="lnum4">   4:</span><span> <span>*@param string $dirTo 目标目录名</span></span>
Copy after login
<span id="lnum5">   5:</span><span> <span>*/</span></span>
Copy after login
Copy after login
<span id="lnum6">   6:</span> function copyDir($dirSrc,$dirTo)
Copy after login
<span id="lnum7">   7:</span><span> {</span>
Copy after login
<span id="lnum8">   8:</span><span>     <span>if</span>(is_file($dirTo))</span>
Copy after login
<span id="lnum9">   9:</span><span>     {</span>
Copy after login
Copy after login
<span id="lnum10">  10:</span><span>         <span>echo</span> <span>'目标目录不能创建'</span>;<span>/*目标不是一个*/</span></span>
Copy after login
<span id="lnum11">  11:</span><span>         <span>return</span>;</span>
Copy after login
<span id="lnum12">  12:</span><span>     }</span>
Copy after login
<span id="lnum13">  13:</span><span>     <span>if</span>(!file_exists($dirTo))</span>
Copy after login
<span id="lnum14">  14:</span>     {
Copy after login
<span id="lnum15">  15:</span><span>         <span>/*目录不存在则创建*/</span></span>
Copy after login
<span id="lnum16">  16:</span><span>         mkdir($dirTo);</span>
Copy after login
<span id="lnum17">  17:</span><span>     }</span>
Copy after login
<span id="lnum18">  18:</span><span>     <span>if</span>($dir_handle = @opendir($dirSrc))</span>
Copy after login
<span id="lnum19">  19:</span><span>     {</span>
Copy after login
<span id="lnum20">  20:</span><span>         <span>while</span> ($fileName = readdir($dir_handle))</span>
Copy after login
<span id="lnum21">  21:</span><span>         {</span>
Copy after login
<span id="lnum22">  22:</span><span>              <span>/*排除两个特殊目录*/</span></span>
Copy after login
<span id="lnum23">  23:</span><span>             <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
Copy after login
<span id="lnum24">  24:</span><span>             {</span>
Copy after login
<span id="lnum25">  25:</span><span>                 $subSrcFile = $dirSrc.<span>'/'</span>.$fileName;</span>
Copy after login
<span id="lnum26">  26:</span><span>                 $subToFile = $dirTo.<span>'/'</span>.$fileName;</span>
Copy after login
<span id="lnum27">  27:</span><span>                 <span>if</span>(is_file($subSrcFile))</span>
Copy after login
<span id="lnum28">  28:</span><span>                 {</span>
Copy after login
<span id="lnum29">  29:</span><span>                     copy($subSrcFile,$subToFile);</span>
Copy after login
<span id="lnum30">  30:</span><span>                 }</span>
Copy after login
<span id="lnum31">  31:</span><span>                 <span>if</span>(is_dir($subSrcFile))</span>
Copy after login
<span id="lnum32">  32:</span><span>                 {</span>
Copy after login
<span id="lnum33">  33:</span><span>                     copyDir($subSrcFile,$subToFile);</span>
Copy after login
<span id="lnum34">  34:</span><span>                 }</span>
Copy after login
<span id="lnum35">  35:</span><span>             }</span>
Copy after login
<span id="lnum36">  36:</span><span>         }</span>
Copy after login
<span id="lnum37">  37:</span><span>         closedir($dir_handle);</span>
Copy after login
<span id="lnum38">  38:</span><span>         <span>return</span> $dirSrc.<span>'目录已经复制到'</span>.$dirTo.<span>'目录'</span>;</span>
Copy after login
<span id="lnum39">  39:</span><span>     }</span>
Copy after login
<span id="lnum40">  40:</span><span> }</span>
Copy after login
<span id="lnum41">  41:</span><span> <span>echo</span> copyDir(<span>'./test'</span>,<span>'../testcopy'</span>);</span>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/885998.htmlTechArticleSummary of basic file and directory operations commonly used in PHP, source: http://www.ido321.com/835. html 1. Basic file operations The basic operations of files are: file judgment, directory judgment, file size...
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