PHP遍历文件目录与驱除目录中的文件
PHP遍历文件目录与清除目录中的文件
今天无聊中练习了一下PHP遍历文件目录的程序,编写了以下两个程序,不过质量不是很好,轻拍~~~
?
1、清除PHP缓存文件
?
?
<?phpfunction read_dir($dir,$file){ $a =strpos($file,".php"); if($a>0) { unlink($dir . $file); echo "delete $dir$file <br>"; return true; } if(strpos($file,".") === 0 || strpos($file,".") !== false ) return true; if(strpos($file,".") === false || strpos($dir,"/") === false) { $dir = $dir . $file . "/"; if(!is_dir($dir)) return false; $dh = opendir($dir); while(($file = readdir($dh)) != false) { read_dir($dir,$file); //递归调用 } }}function clear_caches(){ $dir = "./temp/"; //要清除的PHP缓存文件目录 if(!is_dir($dir)) die("It is not a dir"); $dh = opendir($dir); while(($file = readdir($dh) )!=false) { //var_dump($file); read_dir($dir,$file); }}?>
?
?
2、遍历目录中所有文件
?
<html><head> <meta http-enquiv="Content-Type" content="text/html;charset=gb2312"> <title>查看目录</title></head><body> <table width="600" align="center"> <tr> <th width="50%">文件名</th> <th width="25%">修改时间</th> <th width="25%">文件大小(k)</th> </tr> <?php //$dir = "./admin/"; $dir = "c:/"; $up_dir = "上级目录"; $up_url = $dir; if(isset($_REQUEST['act']) && $_REQUEST['act']=='list_dir') { if(empty($_REQUEST['dir'])) { $up_dir="目录为空!"; } $dir = isset($_REQUEST['dir']) ? $_REQUEST['dir'] : $dir; } if(!is_dir($dir)) { $up_dir="无效目录!"; } ?> <tr> <td colspan="3"> <?php if(strpos($up_dir,"上级目录")!==false) { //if($up_url=="") echo $up_dir; if($dir != "./admin/") { $up_url = substr($dir,0,-1); $k = strrpos($up_url,"/"); $up_url = substr($up_url,0,$k-strlen($up_url)); $up_url = $up_url ."/"; } echo "<a href=\"test.php?act=list_dir&dir=$up_url\">$up_dir</a>"; } else { echo $up_dir; die(); } ?> </td> </tr> <?php $up_dir = $dir; $dh = opendir($dir); while(($file=readdir($dh)) != false) { if($file != "." && $file != ".." && $file != ".svn" ) { if(strpos($file,".") !==false) { $time = date("Y-m-d H:i:s", filectime($dir . $file)); $size = filesize($dir . $file)/1000; echo "<tr><td>$file</td><td>$time</td><td>$size</td></tr>"; } else { $time = date("Y-m-d H:i:s.", filectime($dir . $file)); $size = filesize($dir . $file)/1000; $dir = $dir . $file ."/"; echo "<tr><td><a href =\"test.php?act=list_dir&dir=$dir\">$file</a></td><td>$time</td><td>$size</td></tr>"; $dir = $up_dir; } } } ?> </table><?php die(); ?></body></html>
?
?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

http://zeroneta.iteye.com/blog/1173071