星期六大家好!
所以我有这个PHP代码,显示特定文件夹中的所有文件。一切运行良好,按预期工作。
我只有一个问题,当显示文件时,让我说我想查看我的webbrowser上的特定文件,我点击该文件,我得到了“找不到对象”#39;页。当我查看网址时,我知道为什么找不到该网址,因为网址显示为:http://localhost/studybuddy/maths.txt
但是文件maths.txt位于文件夹' uploads'所以网址必须说: 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]))." ";
// Gets Date Modified Data
$modtime=date("M j Y g:i A", filemtime("./uploads/".$dirArray[$index]))." ";
$timekey=date("YmdHis", filemtime("./uploads/".$dirArray[$index]))." ";
// 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="<Directory>";
$size="<Directory>";
$class="dir";
} else {
$class="file";
}
// Cleans up . and .. directories
if($name=="."){$name=". (Current Directory)"; $extn="<System Dir>";}
if($name==".."){$name=".. (Parent Directory)"; $extn="<System Dir>";}
// 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
答案 0 :(得分:1)
您可以替换
$namehref = $dirArray[$index];
通过
$namehref = "uploads/" . $dirArray[$index];