如何将正确的文件目录位置分配为属性



大家星期六好!

我有这段php代码显示了特定文件夹中的所有文件。一切都运行良好,并按预期工作。

我只有一个问题,当文件显示时,让我说我想在我的网络浏览器上查看一个特定的文件,我点击该文件,我得到"对象未找到"页面。当我查看URL时,我明白了为什么找不到它,因为URL说:http://localhost/studybuddy/maths.txt

但是math .txt文件在'uploads'文件夹中,所以url必须写:http://localhost/studybuddy/uploads/maths.txt,以显示文件

这是我的代码:

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Student Notes</title>
  <link rel="stylesheet" href="style.css">
  <script src=".sorttable.js"></script>
  <style>
  .upload{
    position: absolute;
    top: 400px;
    right: 800px;
    }  
  </style>

</head>
<body>
  <div id="container">
    <h1>Student Notes</h1>
    <table class="sortable">
      <thead>
        <tr>
          <th>Filename</th>
          <th>Type</th>
          <th>Size <small>(bytes)</small></th>
          <th>Date Modified</th>
        </tr>
      </thead>
      <tbody>
      <?php
        // Opens directory
        $myDirectory=opendir("./uploads/");
        // Gets each entry
        while($entryName=readdir($myDirectory)) {
          $dirArray[]=$entryName;
        }
        // Finds extensions of files
        function findexts ($filename) {
          $filename=strtolower($filename);
          //$exts=split("[/\.]", $filename);
          $n=count($exts)-1;
          $exts=$exts[$n];
          return $exts;
        }
        // Closes directory
        closedir($myDirectory);
        // Counts elements in array
        $indexCount=count($dirArray);
        // Sorts files
        sort($dirArray);
        // Loops through the array of files
        for($index=0; $index < $indexCount; $index++) {
          // Allows ./?hidden to show hidden files
          if($_SERVER['QUERY_STRING']=="hidden")
          {$hide="";
          $ahref="./";
          $atext="Hide";}
          else
          {$hide=".";
          $ahref="./?hidden";
          $atext="Show";}
          if(substr("$dirArray[$index]", 0, 1) != $hide) {
          // Gets File Names
          $name=$dirArray[$index];
          $namehref=$dirArray[$index];
          // Gets Extensions 
          $extn=findexts($dirArray[$index]); 
          // Gets file size 
         $size=number_format(filesize("./uploads/".$dirArray[$index]))."&nbsp;";         
          // Gets Date Modified Data
          $modtime=date("M j Y g:i A", filemtime("./uploads/".$dirArray[$index]))."&nbsp;";
          $timekey=date("YmdHis", filemtime("./uploads/".$dirArray[$index]))."&nbsp;";

          // Prettifies File Types, add more to suit your needs.
          switch ($extn){
            case "png": $extn="PNG Image"; break;
            case "jpg": $extn="JPEG Image"; break;
            case "svg": $extn="SVG Image"; break;
            case "gif": $extn="GIF Image"; break;
            case "ico": $extn="Windows Icon"; break;
            case "doc": $extn="MS DOC"; break;
            case "docx": $extn="MS DOCX"; break;
            case "odt": $extn="Open Office DOC"; break;
            case "txt": $extn="Text File"; break;
            case "log": $extn="Log File"; break;
            case "htm": $extn="HTML File"; break;
            case "php": $extn="PHP Script"; break;
            case "js": $extn="Javascript"; break;
            case "css": $extn="Stylesheet"; break;
            case "pdf": $extn="PDF Document"; break;
            case "zip": $extn="ZIP Archive"; break;
            case "bak": $extn="Backup File"; break;
            default: $extn=strtoupper($extn)." File"; break;
          }
          // Separates directories
          if(is_dir($dirArray[$index])) {
            $extn="&lt;Directory&gt;"; 
            $size="&lt;Directory&gt;"; 
            $class="dir";
          } else {
            $class="file";
          }
          // Cleans up . and .. directories 
          if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;";}
          if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";}
          // Print 'em
          print("
          <tr class='$class'>
            <td><a href='./$namehref'>$name</a></td>
            <td><a href='./$namehref'>$extn</a></td>
            <td><a href='./$namehref'>$size</a></td>
            <td sorttable_customkey='$timekey'><a href='./$namehref'>$modtime</a></td>
          </tr>");
          }
        }
      ?>
      </tbody>
    </table>
    <h2></h2>
    <h2><a href='index.html'>Go Home</a></h2>
    <form class="upload" enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000000" />
    <a class="color">Send this file:</a><input name="userfile" type="file" />
    <input type="submit" value="Send File" />
    </form>

  </div>
</body>
</html>

我的问题是,我如何将正确的目录作为前缀分配给文件作为属性?

希望我已经给了你足够的信息。

感谢

- m

可以替换

$namehref = $dirArray[$index];

$namehref = "uploads/" . $dirArray[$index];

相关内容

  • 没有找到相关文章

最新更新