List files and directories
来源:互联网 发布:java \\d是什么 编辑:程序博客网 时间:2024/05/29 10:31
In this tutorial I will show you how to list files in a directory in a pretty way.
This can be a nice way to make files available in a simple way via a web site. There a some security stuff that you need to think about when making stuff like this public. But the way this script works is harmful :-)
We have some helpful functions in PHP for this...
First of all, for security we check that the provided directory really is a directory. For this we have the function is_dir. Pretty simple right.
The next step is maybe a little bit stranger: opendir. This opens a "handler" for the directory. This handler is used for other functions that works against the file system. So it's pretty good to have!
The most complicated row in the script is this one:
while(false !== ($file = readdir($dir)))
In english, this row will run the code inside the { }'s as long as readdir returns files. As long as there are more files in the directory we will get the file name in the variable $file.
<script type="text/javascript"><!--google_ad_client = "pub-6953732755193150";google_alternate_ad_url = "http://www.999tutorials.com/google_adsense_script.html";google_ad_width = 468;google_ad_height = 60;google_ad_format = "468x60_as";google_ad_type = "text";google_ad_channel ="4126591546";google_color_border = "FFFFFF";google_color_bg = "FFFFFF";google_color_link = "DE3866";google_color_url = "000000";google_color_text = "000000";//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
In some cases it is not interested in showing directories in our listing, so I have created a "config variable $listDirectories = true; so you can decide if you want to show directories.
This row takes care of if we are to show text about directories:
if($listDirectories || $type != "dir")
Enough talking... Here is the magic code for you :-)
Set the variable $theDirectory to whatever directory you want to show. The directory "." is the current directory, and ".." is the parent directory.
// This is the directory to list files for.
$theDirectory = ".";
// Do you want to show directories? change to false to hide directories.
$listDirectories = true;
if(is_dir($theDirectory))
{
echo "<table><tr><td>Name</td><td>Type</td><td>Size</td></tr>";
$dir = opendir($theDirectory);
while(false !== ($file = readdir($dir)))
{
$type = filetype($theDirectory ."/". $file);
if($listDirectories || $type != "dir")
{
echo "<tr><td>" . $file . "</td>";
echo "<td>" . $type . "</td>";
echo "<td>";
if($type == "file")
echo filesize($file);
echo "</td></tr>";
}
}
closedir($dir);
echo "</table>";
}
else
{
echo $theDirectory . " is not a directory";
}
?>
I don't think there are more things to say about this script. It shows you the files, directories and their size in a simple table. Should be a perfect PHP function to start with for creating file listings. Good luck with the coding!
- List files and directories
- Files and Directories
- 4-Files and Directories
- Files and Directories
- cp - copy files and directories
- APUE-Chapter4. Files and Directories
- linux 学习笔记之 Files and Directories
- Ignore Files and Directories in Subversion
- Apache Tomcat 6.0 Directories and Files
- BASIC ROOTKIT that hides files, directories, and processes
- BASIC ROOTKIT that hides files, directories, and processes
- BASIC ROOTKIT that hides files, directories, and processes
- BASIC ROOTKIT that hides files, directories, and processes
- Find The Largest Top 10 Files and Directories on Linux
- Linux - 操作文件与目录(manipulating files and directories)
- How to diff and merge files or directories on Linux
- 每天一命令(7)cp (copy files and directories)
- This inspection checks references to files and directories
- 软考有感
- 我的第一篇文章
- 贪吃蛇0.1版
- For the Ray.Charles
- 出来砸到
- List files and directories
- VSS使用手册
- 关注2007维也纳新年音乐会
- windows控制台程序设定光标位置
- [备忘]理清Event Handling的一点思路
- 我的婚纱照片----------看看我跟老婆幸福合影!
- eeee
- 理解HashMap
- 转载:SAP技术概述(三)