Jadual Kandungan
PHP常用的基本文件和目录操作总结,
目录'.$dirName.'下的文件
Rumah pembangunan bahagian belakang tutorial php PHP常用的基本文件和目录操作总结,_PHP教程

PHP常用的基本文件和目录操作总结,_PHP教程

Jul 13, 2016 am 10:18 AM
php dan asas ringkaskan beroperasi dokumen praktikal Jadual kandungan

PHP常用的基本文件和目录操作总结,

来源:http://www.ido321.com/835.html

一、基本文件的操作

文件的基本操作有:文件判断、目录判断、文件大小、读写性判断、存在性判断及文件时间等

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


二、目录的操作

目录的操作有:遍历目录、删除、复制、大小统计等

1、遍历目录

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

结果



2、统计目录大小

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

结果



3、删除目录

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

删除成功的提示信息



4、复制目录

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/885998.htmlTechArticlePHP常用的基本文件和目录操作总结, 来源:http://www.ido321.com/835.html 一、基本文件的操作 文件的基本操作有:文件判断、目录判断、文件大...
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Artikel Panas

Repo: Cara menghidupkan semula rakan sepasukan
1 bulan yang lalu By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Kristal tenaga dijelaskan dan apa yang mereka lakukan (kristal kuning)
2 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Cara mendapatkan biji gergasi
1 bulan yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Konfigurasi Projek CakePHP Konfigurasi Projek CakePHP Sep 10, 2024 pm 05:25 PM

Dalam bab ini, kita akan memahami Pembolehubah Persekitaran, Konfigurasi Umum, Konfigurasi Pangkalan Data dan Konfigurasi E-mel dalam CakePHP.

Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 membawa beberapa ciri baharu, peningkatan keselamatan dan peningkatan prestasi dengan jumlah penamatan dan penyingkiran ciri yang sihat. Panduan ini menerangkan cara memasang PHP 8.4 atau naik taraf kepada PHP 8.4 pada Ubuntu, Debian, atau terbitan mereka

Tarikh dan Masa CakePHP Tarikh dan Masa CakePHP Sep 10, 2024 pm 05:27 PM

Untuk bekerja dengan tarikh dan masa dalam cakephp4, kami akan menggunakan kelas FrozenTime yang tersedia.

Muat naik Fail CakePHP Muat naik Fail CakePHP Sep 10, 2024 pm 05:27 PM

Untuk mengusahakan muat naik fail, kami akan menggunakan pembantu borang. Di sini, adalah contoh untuk muat naik fail.

Penghalaan CakePHP Penghalaan CakePHP Sep 10, 2024 pm 05:25 PM

Dalam bab ini, kita akan mempelajari topik berikut yang berkaitan dengan penghalaan ?

Bincangkan CakePHP Bincangkan CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP ialah rangka kerja sumber terbuka untuk PHP. Ia bertujuan untuk menjadikan pembangunan, penggunaan dan penyelenggaraan aplikasi lebih mudah. CakePHP adalah berdasarkan seni bina seperti MVC yang berkuasa dan mudah difahami. Model, Pandangan dan Pengawal gu

Pengesah Mencipta CakePHP Pengesah Mencipta CakePHP Sep 10, 2024 pm 05:26 PM

Pengesah boleh dibuat dengan menambah dua baris berikut dalam pengawal.

Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Dec 20, 2024 am 11:31 AM

Kod Visual Studio, juga dikenali sebagai Kod VS, ialah editor kod sumber percuma — atau persekitaran pembangunan bersepadu (IDE) — tersedia untuk semua sistem pengendalian utama. Dengan koleksi sambungan yang besar untuk banyak bahasa pengaturcaraan, Kod VS boleh menjadi c

See all articles