Home > Article > Backend Development > Summary of basic file and directory operations commonly used in PHP, _PHP tutorial
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>
<span id="lnum2"> 2:</span><span> header(<span>"content-type"</span>,<span>"text/html;charset=utf-8"</span>);</span>
<span id="lnum3"> 3:</span><span> <span>/*</span></span>
<span id="lnum4"> 4:</span><span> <span>*声明一个函数,传入文件名获取文件属性</span></span>
<span id="lnum5"> 5:</span><span> <span>*@param string $fileName 文件名称</span></span>
<span id="lnum6"> 6:</span><span> <span>*/</span></span>
<span id="lnum7"> 7:</span><span> <span>function</span> getFilePro($fileName)</span>
<span id="lnum8"> 8:</span><span> {</span>
<span id="lnum9"> 9:</span><span> <span>if</span>(!file_exists($fileName))</span>
<span id="lnum10"> 10:</span><span> {</span>
<span id="lnum11"> 11:</span><span> <span>echo</span> <span>'文件不存在<br/>'</span>;</span>
<span id="lnum12"> 12:</span><span> <span>return</span>;</span>
<span id="lnum13"> 13:</span><span> }</span>
<span id="lnum14"> 14:</span><span> <span>/*是否是普通文件*/</span></span>
<span id="lnum15"> 15:</span><span> <span>if</span>(is_file($fileName))</span>
<span id="lnum16"> 16:</span><span> {</span>
<span id="lnum17"> 17:</span><span> <span>echo</span> $fileName.<span>'是一个文件<br/>'</span>;</span>
<span id="lnum18"> 18:</span><span> }</span>
<span id="lnum19"> 19:</span><span> <span>/*是否是目录*/</span></span>
<span id="lnum20"> 20:</span><span> <span>if</span>(is_dir($fileName))</span>
<span id="lnum21"> 21:</span><span> {</span>
<span id="lnum22"> 22:</span><span> <span>echo</span> $fileName.<span>'是一个目录'</span>;</span>
<span id="lnum23"> 23:</span><span> }</span>
<span id="lnum24"> 24:</span><span> <span>/*输出文件的型态*/</span></span>
<span id="lnum25"> 25:</span><span> <span>echo</span> <span>'文件型态是:'</span>.getFileType($fileName).<span>'<br/>'</span>;</span>
<span id="lnum26"> 26:</span><span> <span>/*文件大小,转换单位*/</span></span>
<span id="lnum27"> 27:</span><span> <span>echo</span> <span>'文件大小是:'</span>.getFileSize(filesize($fileName)).<span>'<br/>'</span>;</span>
<span id="lnum28"> 28:</span><span> <span>/*文件是否可读*/</span></span>
<span id="lnum29"> 29:</span><span> <span>if</span>(is_readable($fileName))</span>
<span id="lnum30"> 30:</span><span> {</span>
<span id="lnum31"> 31:</span><span> <span>echo</span> <span>'文件可读<br/>'</span>;</span>
<span id="lnum32"> 32:</span><span> }</span>
<span id="lnum33"> 33:</span><span> <span>/*文件是否可写*/</span></span>
<span id="lnum34"> 34:</span><span> <span>if</span>(is_writable($fileName))</span>
<span id="lnum35"> 35:</span><span> {</span>
<span id="lnum36"> 36:</span><span> <span>echo</span> <span>'文件可写<br/>'</span>;</span>
<span id="lnum37"> 37:</span><span> }</span>
<span id="lnum38"> 38:</span><span> <span>/*文件是否可执行*/</span></span>
<span id="lnum39"> 39:</span><span> <span>if</span>(is_executable($fileName))</span>
<span id="lnum40"> 40:</span><span> {</span>
<span id="lnum41"> 41:</span><span> <span>echo</span> <span>'文件可执行<br/>'</span>;</span>
<span id="lnum42"> 42:</span><span> }</span>
<span id="lnum43"> 43:</span>
<span id="lnum44"> 44:</span><span> <span>echo</span> <span>'文件创立时间:'</span>.date(<span>'Y年m月j日'</span>,filectime($fileName)).<span>'<br/>'</span>;</span>
<span id="lnum45"> 45:</span><span> <span>echo</span> <span>'文件最后修改时间:'</span>.date(<span>'Y年m月j日'</span>,filemtime($fileName)).<span>'<br/>'</span>;</span>
<span id="lnum46"> 46:</span><span> <span>echo</span> <span>'文件最后打开时间:'</span>.date(<span>'Y年m月j日'</span>,fileatime($fileName)).<span>'<br/>'</span>;</span>
<span id="lnum47"> 47:</span><span> }</span>
<span id="lnum48"> 48:</span>
<span id="lnum49"> 49:</span><span> <span>/*</span></span>
<span id="lnum50"> 50:</span><span> <span>*声明一个函数,返回文件类型</span></span>
<span id="lnum51"> 51:</span><span> <span>*@param string $fileName 文件名称</span></span>
<span id="lnum52"> 52:</span><span> <span>*/</span></span>
<span id="lnum53"> 53:</span><span> <span>function</span> getFileType($fileName)</span>
<span id="lnum54"> 54:</span><span> {</span>
<span id="lnum55"> 55:</span><span> $type = <span>''</span>;</span>
<span id="lnum56"> 56:</span><span> <span>switch</span>(filetype($fileName))</span>
<span id="lnum57"> 57:</span><span> {</span>
<span id="lnum58"> 58:</span><span> <span>case</span> <span>'file'</span>:$type .= <span>'普通文件'</span>;<span>break</span>;</span>
<span id="lnum59"> 59:</span><span> <span>case</span> <span>'dir'</span>:$type .= <span>'目录文件'</span>;<span>break</span>;</span>
<span id="lnum60"> 60:</span><span> <span>case</span> <span>'block'</span>:$type .= <span>'块设备文件'</span>;<span>break</span>;</span>
<span id="lnum61"> 61:</span><span> <span>case</span> <span>'char'</span>:$type .= <span>'字符设备文件'</span>;<span>break</span>;</span>
<span id="lnum62"> 62:</span><span> <span>case</span> <span>'filo'</span>:$type .= <span>'管道文件'</span>;<span>break</span>;</span>
<span id="lnum63"> 63:</span><span> <span>case</span> <span>'link'</span>:$type .= <span>'符号链接'</span>;<span>break</span>;</span>
<span id="lnum64"> 64:</span><span> <span>case</span> <span>'unknown'</span>:$type .= <span>'未知文件'</span>;<span>break</span>;</span>
<span id="lnum65"> 65:</span><span> <span>default</span>:</span>
<span id="lnum66"> 66:</span><span> }</span>
<span id="lnum67"> 67:</span><span> <span>return</span> $type;</span>
<span id="lnum68"> 68:</span><span> }</span>
<span id="lnum69"> 69:</span>
<span id="lnum70"> 70:</span><span> <span>/*</span></span>
<span id="lnum71"> 71:</span><span> <span>*声明一个函数,返回文件大小</span></span>
<span id="lnum72"> 72:</span><span> <span>*@param int $bytes 文件字节数</span></span>
<span id="lnum73"> 73:</span><span> <span>*/</span></span>
<span id="lnum74"> 74:</span><span> <span>function</span> getFileSize($bytes)</span>
<span id="lnum75"> 75:</span><span> {</span>
<span id="lnum76"> 76:</span><span> <span>if</span>($bytes >= pow(2,40))</span>
<span id="lnum77"> 77:</span><span> {</span>
<span id="lnum78"> 78:</span><span> $<span>return</span> = round($bytes / pow(1024,4),2);</span>
<span id="lnum79"> 79:</span><span> $suffix = <span>'TB'</span>;</span>
<span id="lnum80"> 80:</span><span> }</span>
<span id="lnum81"> 81:</span><span> <span>else</span> <span>if</span>($bytes >= pow(2,30))</span>
<span id="lnum82"> 82:</span><span> {</span>
<span id="lnum83"> 83:</span><span> $<span>return</span> = round($bytes / pow(1024,3),2);</span>
<span id="lnum84"> 84:</span><span> $suffix = <span>'GB'</span>;</span>
<span id="lnum85"> 85:</span><span> }</span>
<span id="lnum86"> 86:</span><span> <span>else</span> <span>if</span>($bytes >= pow(2,20))</span>
<span id="lnum87"> 87:</span><span> {</span>
<span id="lnum88"> 88:</span><span> $<span>return</span> = round($bytes / pow(1024,2),2);</span>
<span id="lnum89"> 89:</span><span> $suffix = <span>'MB'</span>;</span>
<span id="lnum90"> 90:</span><span> }</span>
<span id="lnum91"> 91:</span><span> <span>else</span> <span>if</span>($bytes >= pow(2,10))</span>
<span id="lnum92"> 92:</span><span> {</span>
<span id="lnum93"> 93:</span><span> $<span>return</span> = round($bytes / pow(1024,1),2);</span>
<span id="lnum94"> 94:</span><span> $suffix = <span>'KB'</span>;</span>
<span id="lnum95"> 95:</span><span> }</span>
<span id="lnum96"> 96:</span><span> <span>else</span></span>
<span id="lnum97"> 97:</span><span> {</span>
<span id="lnum98"> 98:</span><span> $<span>return</span> = $bytes;</span>
<span id="lnum99"> 99:</span><span> $suffix = <span>'B'</span>;</span>
<span id="lnum100"> 100:</span><span> }</span>
<span id="lnum101"> 101:</span><span> <span>return</span> $<span>return</span>.<span>" "</span>.$suffix;</span>
<span id="lnum102"> 102:</span><span> }</span>
<span id="lnum103"> 103:</span>
<span id="lnum104"> 104:</span><span> <span>/*调用函数,传入test目录下的test.txt文件*/</span></span>
<span id="lnum105"> 105:</span><span> getFilePro(<span>'./test/div.html'</span>);</span>
<span id="lnum106"> 106:</span><span> ?></span>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>
<span id="lnum2"> 2:</span><span> <span>*遍历目录</span></span>
<span id="lnum3"> 3:</span><span> <span>*@param string $dirName 目录名</span></span>
<span id="lnum4"> 4:</span><span> <span>*/</span></span>
<span id="lnum5"> 5:</span><span> <span>function</span> findDir($dirName)</span>
<span id="lnum6"> 6:</span><span> {</span>
<span id="lnum7"> 7:</span><span> $num = 0; <span>/*统计子文件个数*/</span></span>
<span id="lnum8"> 8:</span><span> $dir_handle = opendir($dirName); <span>/*打开目录*/</span></span>
<span id="lnum9"> 9:</span><span> <span>/*输出目录文件*/</span></span>
<span id="lnum10"> 10:</span><span> <span>echo</span> <span>'<table border="0" align="center" width="600" cellspacing="0" cellpadding="0">'</span>;</span>
<span id="lnum11"> 11:</span><span> <span>echo</span> <span>'<caption><h2>目录'</span>.$dirName.<span>'下的文件</h2></caption>'</span>;</span>
<span id="lnum12"> 12:</span><span> <span>echo</span> <span>'<tr align="left" bgcolor="#cccccc"'</span>;</span>
<span id="lnum13"> 13:</span><span> <span>echo</span> <span>'<th>文件名</th><th>文件大小</th><th>文件类型</th><th>修改时间</th></tr>'</span>;</span>
<span id="lnum14"> 14:</span>
<span id="lnum15"> 15:</span><span> <span>while</span>($file = readdir($dir_handle))</span>
<span id="lnum16"> 16:</span><span> {</span>
<span id="lnum17"> 17:</span><span> $dirFile = $dirName.<span>'/'</span>.$file;</span>
<span id="lnum18"> 18:</span><span> $bgcolor = $num++%2==0?<span>'#ffffff'</span>:<span>'#cccccc'</span>;</span>
<span id="lnum19"> 19:</span><span> <span>echo</span> <span>'<tr bgcolor='</span>.$bgcolor.<span>'>'</span>;</span>
<span id="lnum20"> 20:</span><span> <span>echo</span> <span>'<td>'</span>.$file.<span>'</td>'</span>;</span>
<span id="lnum21"> 21:</span><span> <span>echo</span> <span>'<td>'</span>.filesize($dirFile).<span>'</td>'</span>;</span>
<span id="lnum22"> 22:</span><span> <span>echo</span> <span>'<td>'</span>.filetype($dirFile).<span>'</td>'</span>;</span>
<span id="lnum23"> 23:</span><span> <span>echo</span> <span>'<td>'</span>.date(<span>'Y/n/t'</span>,filemtime($dirFile)).<span>'</td>'</span>;</span>
<span id="lnum24"> 24:</span><span> <span>echo</span> <span>'</tr>'</span>;</span>
<span id="lnum25"> 25:</span><span> }</span>
<span id="lnum26"> 26:</span><span> <span>echo</span> <span>"</table>"</span>;</span>
<span id="lnum27"> 27:</span><span> closedir($dir_handle); <span>/*关闭目录*/</span></span>
<span id="lnum28"> 28:</span><span> <span>echo</span> <span>"在<b>"</span>.$dirName.<span>"</b>目录下共有<b>"</span>.$num.<span>'</b>个子文件'</span>;</span>
<span id="lnum29"> 29:</span><span> }</span>
<span id="lnum30"> 30:</span><span> <span>/*传递当前目录下的test目录*/</span></span>
<span id="lnum31"> 31:</span><span> findDir(<span>'./test'</span>);</span>
Results
2. Statistical directory size
<span id="lnum1"> 1:</span><span> <span>/*</span></span>
<span id="lnum2"> 2:</span><span> <span>*统计目录大小</span></span>
<span id="lnum3"> 3:</span><span> <span>*@param string $dirName 目录名</span></span>
<span id="lnum4"> 4:</span><span> <span>*@return double</span></span>
<span id="lnum5"> 5:</span><span> <span>*/</span></span>
<span id="lnum6"> 6:</span>
<span id="lnum7"> 7:</span><span> <span>function</span> dirSize($dirName)</span>
<span id="lnum8"> 8:</span><span> {</span>
<span id="lnum9"> 9:</span><span> $dir_size = 0;</span>
<span id="lnum10"> 10:</span><span> <span>if</span>($dir_handle = @opendir($dirName))</span>
<span id="lnum11"> 11:</span><span> {</span>
<span id="lnum12"> 12:</span><span> <span>while</span> ($fileName = readdir($dir_handle))</span>
<span id="lnum13"> 13:</span><span> {</span>
<span id="lnum14"> 14:</span> /*排除两个特殊目录*/
<span id="lnum15"> 15:</span><span> <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
<span id="lnum16"> 16:</span><span> {</span>
<span id="lnum17"> 17:</span><span> $subFile = $dirName.<span>'/'</span>.$fileName;</span>
<span id="lnum18"> 18:</span><span> <span>if</span>(is_file($subFile))</span>
<span id="lnum19"> 19:</span><span> {</span>
<span id="lnum20"> 20:</span><span> $dir_size += filesize($subFile);</span>
<span id="lnum21"> 21:</span><span> }</span>
<span id="lnum22"> 22:</span><span> <span>if</span>(is_dir($subFile))</span>
<span id="lnum23"> 23:</span><span> {</span>
<span id="lnum24"> 24:</span><span> $dir_size += dirSize($subFile);</span>
<span id="lnum25"> 25:</span><span> }</span>
<span id="lnum26"> 26:</span><span> }</span>
<span id="lnum27"> 27:</span><span> }</span>
<span id="lnum28"> 28:</span><span> closedir($dir_handle);</span>
<span id="lnum29"> 29:</span><span> <span>return</span> $dir_size;</span>
<span id="lnum30"> 30:</span><span> }</span>
<span id="lnum31"> 31:</span><span> }</span>
<span id="lnum32"> 32:</span><span> <span>/*传递当前目录下的test目录*/</span></span>
<span id="lnum33"> 33:</span><span> $dir_size = dirSize(<span>'./test'</span>);</span>
<span id="lnum34"> 34:</span><span> <span>echo</span> <span>'./test目录文件大小是:'</span>.round($dir_size / pow(1024,1),2).<span>'KB'</span>;</span>
Results
3. Delete directory
<span id="lnum1"> 1:</span><span> <span>/*</span></span>
<span id="lnum2"> 2:</span><span> <span>*删除目录</span></span>
<span id="lnum3"> 3:</span><span> <span>*@param string $dirName 目录名</span></span>
<span id="lnum4"> 4:</span><span> <span>*/</span></span>
<span id="lnum5"> 5:</span><span> <span>function</span> delDir($dirName)</span>
<span id="lnum6"> 6:</span><span> {</span>
<span id="lnum7"> 7:</span><span> <span>/*php中的mkdir函数就可以创建目录*/</span></span>
<span id="lnum8"> 8:</span><span> <span>if</span>(file_exists($dirName))</span>
<span id="lnum9"> 9:</span><span> {</span>
<span id="lnum10"> 10:</span><span> <span>if</span>($dir_handle = @opendir($dirName))</span>
<span id="lnum11"> 11:</span><span> {</span>
<span id="lnum12"> 12:</span><span> <span>while</span> ($fileName = readdir($dir_handle))</span>
<span id="lnum13"> 13:</span><span> {</span>
<span id="lnum14"> 14:</span> /*排除两个特殊目录*/
<span id="lnum15"> 15:</span><span> <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
<span id="lnum16"> 16:</span><span> {</span>
<span id="lnum17"> 17:</span><span> $subFile = $dirName.<span>'/'</span>.$fileName;</span>
<span id="lnum18"> 18:</span><span> <span>if</span>(is_file($subFile))</span>
<span id="lnum19"> 19:</span><span> {</span>
<span id="lnum20"> 20:</span><span> unlink($subFile);</span>
<span id="lnum21"> 21:</span><span> }</span>
<span id="lnum22"> 22:</span><span> <span>if</span>(is_dir($subFile))</span>
<span id="lnum23"> 23:</span><span> {</span>
<span id="lnum24"> 24:</span><span> delDir($subFile);</span>
<span id="lnum25"> 25:</span><span> }</span>
<span id="lnum26"> 26:</span><span> }</span>
<span id="lnum27"> 27:</span><span> }</span>
<span id="lnum28"> 28:</span><span> closedir($dir_handle);</span>
<span id="lnum29"> 29:</span><span> rmdir($dirName);</span>
<span id="lnum30"> 30:</span><span> <span>return</span> $dirName.<span>'目录已经删除'</span>;</span>
<span id="lnum31"> 31:</span><span> }</span>
<span id="lnum32"> 32:</span><span> }</span>
<span id="lnum33"> 33:</span><span> }</span>
<span id="lnum34"> 34:</span><span> <span>/*传递test目录的副本test1*/</span></span>
<span id="lnum35"> 35:</span><span> <span>echo</span> delDir(<span>'./test1'</span>);</span>
Prompt message for successful deletion
4. Copy directory
<span id="lnum1"> 1:</span><span> <span>/*</span></span>
<span id="lnum2"> 2:</span><span> <span>*复制目录</span></span>
<span id="lnum3"> 3:</span><span> <span>*@param string $dirSrc 原目录名</span></span>
<span id="lnum4"> 4:</span><span> <span>*@param string $dirTo 目标目录名</span></span>
<span id="lnum5"> 5:</span><span> <span>*/</span></span>
<span id="lnum6"> 6:</span> function copyDir($dirSrc,$dirTo)
<span id="lnum7"> 7:</span><span> {</span>
<span id="lnum8"> 8:</span><span> <span>if</span>(is_file($dirTo))</span>
<span id="lnum9"> 9:</span><span> {</span>
<span id="lnum10"> 10:</span><span> <span>echo</span> <span>'目标目录不能创建'</span>;<span>/*目标不是一个*/</span></span>
<span id="lnum11"> 11:</span><span> <span>return</span>;</span>
<span id="lnum12"> 12:</span><span> }</span>
<span id="lnum13"> 13:</span><span> <span>if</span>(!file_exists($dirTo))</span>
<span id="lnum14"> 14:</span> {
<span id="lnum15"> 15:</span><span> <span>/*目录不存在则创建*/</span></span>
<span id="lnum16"> 16:</span><span> mkdir($dirTo);</span>
<span id="lnum17"> 17:</span><span> }</span>
<span id="lnum18"> 18:</span><span> <span>if</span>($dir_handle = @opendir($dirSrc))</span>
<span id="lnum19"> 19:</span><span> {</span>
<span id="lnum20"> 20:</span><span> <span>while</span> ($fileName = readdir($dir_handle))</span>
<span id="lnum21"> 21:</span><span> {</span>
<span id="lnum22"> 22:</span><span> <span>/*排除两个特殊目录*/</span></span>
<span id="lnum23"> 23:</span><span> <span>if</span>($fileName != <span>'.'</span> && $fileName != <span>'..'</span>)</span>
<span id="lnum24"> 24:</span><span> {</span>
<span id="lnum25"> 25:</span><span> $subSrcFile = $dirSrc.<span>'/'</span>.$fileName;</span>
<span id="lnum26"> 26:</span><span> $subToFile = $dirTo.<span>'/'</span>.$fileName;</span>
<span id="lnum27"> 27:</span><span> <span>if</span>(is_file($subSrcFile))</span>
<span id="lnum28"> 28:</span><span> {</span>
<span id="lnum29"> 29:</span><span> copy($subSrcFile,$subToFile);</span>
<span id="lnum30"> 30:</span><span> }</span>
<span id="lnum31"> 31:</span><span> <span>if</span>(is_dir($subSrcFile))</span>
<span id="lnum32"> 32:</span><span> {</span>
<span id="lnum33"> 33:</span><span> copyDir($subSrcFile,$subToFile);</span>
<span id="lnum34"> 34:</span><span> }</span>
<span id="lnum35"> 35:</span><span> }</span>
<span id="lnum36"> 36:</span><span> }</span>
<span id="lnum37"> 37:</span><span> closedir($dir_handle);</span>
<span id="lnum38"> 38:</span><span> <span>return</span> $dirSrc.<span>'目录已经复制到'</span>.$dirTo.<span>'目录'</span>;</span>
<span id="lnum39"> 39:</span><span> }</span>
<span id="lnum40"> 40:</span><span> }</span>
<span id="lnum41"> 41:</span><span> <span>echo</span> copyDir(<span>'./test'</span>,<span>'../testcopy'</span>);</span>