我在如何做到这一点上有一个真正的脱节——部分原因是我没有使用文件系统的经验。我正在尝试创建一个脚本来导航文件夹下面的文件。该文件当前通过列出其所在目录的内容(又名“.”)来工作。我不知道如何让它列出下面文件夹的内容以及最好的方法。我的脚本需要如何修改才能达到这个目标?
[代码]
// sets ordering variables
$a = $_GET['a'];
// are you allowed to delete?
if($_SESSION['delete'] == "ON") {
$do_del = true;
}
else {
$do_del = false;
}
// scandir to opendir coversion from stackoverflow.com/questions/6823489/
$files_dir = ".";
$dir_handle = opendir($files_dir);
while ($dir_temp = readdir($dir_handle)) {
$arr_dir[] = $dir_temp;
}
closedir($dir_handle);
// function created by "acecream" on php.net; modified to include reverse sort
function sortArray($array, $key, $reverse = false)
{
if(isset($array)) {
foreach ($array as $i => $k)
{ $sort_values[$i] = $array[$i][$key]; }
if ($reverse == true)
{ arsort ($sort_values); }
else
{ asort ($sort_values); }
reset ($sort_values);
while (list ($arr_key, $arr_val) = each ($sort_values))
{ $sorted_arr[] = $array[$arr_key]; }
return $sorted_arr;
}
}
foreach($arr_dir as $file)
{
if(!preg_match("/(_vti)|(_borders)|(_private)|(hidden)|(~_)|(\.php)/", $file) && ($file != ".") && ($file !=".."))
{
$full = "$files_dir/$file";
$name = strval($file);
$size = sprintf("%u", filesize($full));
$kind = pathinfo($file, PATHINFO_EXTENSION); // strval(end(explode(".", $file)));
$mtime = filemtime($full); // modify date
$ctime = filectime($full); // creation date
$arr_files[] = array($name, $size, $kind, $mtime, $ctime);
}
}
$arr_count = count($arr_files);
switch ($a) {
case 1 : $arr_files = sortArray($arr_files, 0); break;
case 2 : $arr_files = sortArray($arr_files, 0, true); break;
case 3 : $arr_files = sortArray($arr_files, 1); break;
case 4 : $arr_files = sortArray($arr_files, 1, true); break;
case 5 : $arr_files = sortArray($arr_files, 2); break;
case 6 : $arr_files = sortArray($arr_files, 2, true); break;
case 7 : $arr_files = sortArray($arr_files, 3); break;
case 8 : $arr_files = sortArray($arr_files, 3, true); break;
default: $arr_files = sortArray($arr_files, 3, true); break;
}
unset($a);
function myFileSize($value) {
// convert raw byte site to friendlier forms
if($value > 1000) { $sizestr = round(($value/1024), 1) . " KB"; }
else { $sizestr = "$value B"; }
if($value > 1000000) { $sizestr = round(($value/1048576), 1) . " MB"; }
if($value > 1000000000) { $sizestr = round(($value/1073741824), 1) . " GB"; }
return $sizestr;
}
function myDate($value) {
$date = date("j M y G:i", $value);
return $date;
}
if($arr_count > 0)
{
if($do_del == true) {
print "<form method=\"post\" action=\"./?d=delete\">\n";
}
print "<table>\n";
print " <tr>\n";
print " <td>File Name</td>\n";
print " <td>Size</td>\n";
print " <td>Type</td>\n";
print " <td>Modified</td>\n";
print " <td>Created</td>\n";
if($do_del == true) {
print " <td>Delete?</td>\n";
}
print " </tr>\n";
print " <tr style=\"vertical-align: middle; background-color: #fff; border: 1px solid #000;\">\n";
print " <td>\n";
print " <a href=\"./?a=1\"><img src=\"./~_arr-asc.gif\" border=\"0\"></a>\n";
print " <a href=\"./?a=2\"><img src=\"./~_arr-dsc.gif\" border=\"0\"></a>\n";
print " </td>\n";
print " <td>\n";
print " <a href=\"./?a=3\"><img src=\"./~_arr-asc.gif\" border=\"0\"></a>\n";
print " <a href=\"./?a=4\"><img src=\"./~_arr-dsc.gif\" border=\"0\"></a>\n";
print " </td>\n";
print " <td>\n";
print " <a href=\"./?a=5\"><img src=\"./~_arr-asc.gif\" border=\"0\"></a>\n";
print " <a href=\"./?a=6\"><img src=\"./~_arr-dsc.gif\" border=\"0\"></a>\n";
print " </td>\n";
print " <td>\n";
print " <a href=\"./?a=7\"><img src=\"./~_arr-asc.gif\" border=\"0\"></a>\n";
print " <a href=\"./?a=8\"><img src=\"./~_arr-dsc.gif\" border=\"0\"></a>\n";
print " </td>\n";
print " <td> </td>\n";
if($do_del == true) {
print " <td> </td>\n";
}
print " </tr>\n";
for ($beat = 0; $beat < $arr_count; $beat++)
{
$name = $arr_files[$beat][0];
$size = $arr_files[$beat][1];
$ext = strtolower($arr_files[$beat][2]);
$mdate = $arr_files[$beat][3];
$cdate = $arr_files[$beat][4];
$size = myFileSize($size);
$mdate = myDate($mdate);
$cdate = myDate($cdate);
// pick the image for file type based on file extension
if(is_dir($name)) {
$pic = "~_folder.png";
}
else {
$pic = "~_file.png";
}
$url = "<a href=\"$files_dir/$name\">$name</a>"; // create hard link
print " <tr>\n";
print " <td style=\"width: 300px;\"><img src=\"./$pic\"> $url</td>\n";
print " <td style=\"width: 75px;\">$size</td>\n";
print " <td style=\"width: 50px;\">$ext</td>\n";
print " <td style=\"width: 150px;\">$mdate</td>\n";
print " <td style=\"width: 150px;\">$cdate</td>\n";
if($do_del == true) {
print " <td><input type=\"checkbox\" name=\"list[$files_dir/$name]\" value=\"ON\"></td>\n";
}
print " </tr>\n";
}
print "</table>\n";
if($do_del == true) {
print " <p><input name=\"Submit1\" type=\"submit\" value=\"submit\">\n";
print " <input name=\"Reset1\" type=\"reset\" value=\"reset\"></p>\n";
print "</form>\n";
}
}
else
{
print "There are no files available for download.";
}
[/代码]