猿问

如何更改表格内容而不是转到目录

我完成了一个列出当前目录的表格。我希望的是,当我单击目录时,表格会刷新并加载目录的内容,而不是转到实际目录。我应该使用的正确代码是什么?


<?php


function view_size($size)

{

    if($size >= 1073741824)

    {

        $size = @round($size / 1073741824 * 100) / 100 . " GB";

    }

    elseif($size >= 1048576)

    {

        $size = @round($size / 1048576 * 100) / 100 . " MB";

    }

    elseif($size >= 1024)

    {

        $size = @round($size / 1024 * 100) / 100 . " KB";

    }

    else

    {

        $size = $size . " B";

    }

    return $size;

}


function dirlist()

{

    $myDirectory = opendir(".");


    while($entries = readdir($myDirectory))

    {

        $dirListArray[] = $entries;

    }


    $fileCount = count($dirListArray);

    sort($dirListArray);

    print("<p style='color:#CCC;padding:0px;margin:5px;'>$fileCount FILES / FOLDER FOUND</p>");


    print("<table style='color:#FFF' width=100% border=1 cellpadding=5 cellspacing=0 class=whitelinks>\n");

    print("<tr><th>FILE/FOLDER NAME</th><th>FILE TYPE</th><th>FILE SIZE</th></tr>");

    for($index=0;$index<$fileCount;$index++)

    {

        print("<tr><td><a href='./$dirListArray[$index]/.'>$dirListArray[$index]</a></td>");

        print("<td>");

        print(filetype($dirListArray[$index]));

        print("</td>");

        print("<td>");

        print(view_size(filesize($dirListArray[$index])));

        print("</td>");

        print("</tr>\n");

    }

    print("</table>");

}


?>


<html>

<head>


<style type="text/css">

#directory-list-container {

    margin: 7px;

    padding: 0px;

    border: 3px solid #000;

    outline: 1px solid #666;

}


#directory-list-content {

    margin: 0px;

    padding: 5px;

    border: 2px solid #666;

}


#directory-list-content a {

    color: #FFF;

    text-decoration: none;

}


#directory-list-container a:link {

    color: #FFF;

    text-decoration: none;

}


#directory-list-container a:hover {

    color: #0F0;

    text-decoration: none;

}


#directory-list-container a:active {

    color: #090;

    text-decoration: none;

}


它显示为一个表格,但是当我单击目录或文件时,它会转到该目录或打开文件而不是刷新表格以列出我单击的目录。


SMILET
浏览 191回答 1
1回答

翻翻过去那场雪

当您单击一个链接时,它会在您的浏览器中导航,除非您在服务器上设置了 URL 重写,否则将加载该 URL 上的任何内容。您需要使用 GET(查询)参数将您选择的目录传递给您的 PHP 文件,并在显示锚点时使用它。我对下面的代码有一些自由,但我相信它可以满足您的需求。脚本的名称是从从任何查询参数中剥离的当前 URL 中读取的,并且dir查询参数用于将文件夹名称传递给锚点中的脚本。将这些结合起来,URL 看起来像这样,具体取决于您如何运行它:/folder/script.php?dir=foldername%2Ffolder2请注意,该值是 URL 编码的,这通常是一种很好的做法,每当您将任意字符串作为查询参数传递时。在下面的代码中,文件名输出也包含在htmlspecialchars()其中转义浏览器可能识别为标记并尝试解析的任何 HTML 字符,从而导致页面分崩离析。为了增加安全性,我调用了在运行时ini_set将open_basedir设置更改为当前目录的调用,以避免目录遍历攻击,例如?dir=../../../../etc/passwd可用于访问敏感系统信息的攻击。虽然不是您最初的要求,但我相信这也应该在您的情况下处理,我强烈建议保留此安全措施。此外,还对点文件(.和..)进行了一些处理,以确保未列出当前目录,并且您无法从基本目录向上导航。我还使用HEREDOC使代码比多次调用print. 随之而来的唯一小不便是您不能在字符串中放入太复杂的表达式,因此您必须将它们移动到上面的变量中,但我相信这也有助于提高可读性。<?php// Credit to http://jeffreysambells.com/2012/10/25/human-readable-filesize-phpfunction human_file_size($bytes, $decimals = 2) {&nbsp; $size = array('B', 'KB', 'MB', 'GB');&nbsp; $factor = (int)floor((strlen($bytes) - 1) / 3);&nbsp; return round($bytes / pow(1024, $factor), 2).' '.@$size[$factor];}function dirlist() {&nbsp; // Prevent malicious users from reading files in directories above&nbsp; ini_set('open_basedir', __DIR__);&nbsp; $baseDirectory = '.'.DIRECTORY_SEPARATOR;&nbsp; // Get directory from query parameter&nbsp; $directoryPath = $baseDirectory.(!empty($_GET['dir']) ? rtrim($_GET['dir'], '\\/').DIRECTORY_SEPARATOR : '');&nbsp; $myDirectory = opendir($directoryPath);&nbsp; $isTopLevel = $directoryPath === $baseDirectory;&nbsp; while ($entry = readdir($myDirectory)){&nbsp; &nbsp; if ($entry === '.' || ($isTopLevel && $entry === '..')){&nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; $dirListArray[] = $entry;&nbsp; }&nbsp; $fileCount = count($dirListArray);&nbsp; sort($dirListArray);&nbsp; print <<<HTML&nbsp; &nbsp; <p class="heading">$fileCount FILES / FOLDER FOUND</p>&nbsp; &nbsp; <table width="100%" border="1" cellpadding="5" cellspacing="0" class="whitelinks">&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>FILE/FOLDER NAME</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>FILE TYPE</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>FILE SIZE</th>&nbsp; &nbsp; &nbsp; </tr>HTML;&nbsp; // Get current URL without query parameters&nbsp; // Trim everything after and including "?"&nbsp; $scriptPath = strtok($_SERVER['REQUEST_URI'], '?');&nbsp; foreach ($dirListArray as $indexValue){&nbsp; &nbsp; $htmlEncodedIndex = htmlspecialchars($indexValue);&nbsp; &nbsp; $fileType = filetype($directoryPath.$indexValue);&nbsp; &nbsp; $fileSize = human_file_size(filesize($directoryPath.$indexValue));&nbsp; &nbsp; if ($fileType === 'dir'){&nbsp; &nbsp; &nbsp; if ($indexValue === '..'){&nbsp; &nbsp; &nbsp; &nbsp; // Link to top level, no rectory separator in string&nbsp; &nbsp; &nbsp; &nbsp; if (strpos($indexValue, DIRECTORY_SEPARATOR) === false)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $queryParam = '';&nbsp; &nbsp; &nbsp; &nbsp; // Link to subdirectory&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $parts = explode(DIRECTORY_SEPARATOR, $indexValue);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_pop($parts);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Assemble query param (make sure to URL encode!)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $queryParam = '?dir='.urlencode(implode(DIRECTORY_SEPARATOR, $parts));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; // Assemble query param (make sure to URL encode!)&nbsp; &nbsp; &nbsp; else $queryParam = '?dir='.urlencode($indexValue);&nbsp; &nbsp; &nbsp; $href = $scriptPath.$queryParam;&nbsp; &nbsp; }&nbsp; &nbsp; else $href = $directoryPath.$indexValue;&nbsp; &nbsp; print <<<HTML&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href='$href'>$htmlEncodedIndex</a>&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <td>$fileType</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>$fileSize</td>&nbsp; &nbsp; &nbsp; </tr>HTML;&nbsp; }&nbsp; print '</table>';}?><!DOCTYPE html><html><head>&nbsp; <style type="text/css">&nbsp; &nbsp; .heading {&nbsp; &nbsp; &nbsp; color: #CCC;&nbsp; &nbsp; &nbsp; padding: 0;&nbsp; &nbsp; &nbsp; margin: 5px;&nbsp; &nbsp; }&nbsp; &nbsp; #directory-list-container {&nbsp; &nbsp; &nbsp; margin: 7px;&nbsp; &nbsp; &nbsp; padding: 0;&nbsp; &nbsp; &nbsp; border: 3px solid #000;&nbsp; &nbsp; &nbsp; outline: 1px solid #666;&nbsp; &nbsp; }&nbsp; &nbsp; #directory-list-content {&nbsp; &nbsp; &nbsp; margin: 0;&nbsp; &nbsp; &nbsp; padding: 5px;&nbsp; &nbsp; &nbsp; border: 2px solid #666;&nbsp; &nbsp; }&nbsp; &nbsp; #directory-list-content a,&nbsp; &nbsp; #directory-list-container a:link {&nbsp; &nbsp; &nbsp; color: #00f;&nbsp; &nbsp; &nbsp; text-decoration: none !important;&nbsp; &nbsp; }&nbsp; &nbsp; #directory-list-container a:hover {&nbsp; &nbsp; &nbsp; color: #0F0;&nbsp; &nbsp; }&nbsp; &nbsp; #directory-list-container a:active {&nbsp; &nbsp; &nbsp; color: #090;&nbsp; &nbsp; }&nbsp; &nbsp; #directory-list-container a:visited {&nbsp; &nbsp; &nbsp; color: #DDD;&nbsp; &nbsp; }&nbsp; </style></head><body><div id="directory-list-container">&nbsp; <div id="directory-list-content">&nbsp; &nbsp; <?php dirlist(); ?>&nbsp; </div></div></body></html>
随时随地看视频慕课网APP
我要回答