我正在尝试创建一个文件浏览器,并且我试图找到当用户点击目录时如何调用函数。为了开始,我尝试在类部件上使用onClick(在FUNCTION folderSize之前),让我们打开testDir内部的路径,如果我点击列表中的任何文件或目录,但它什么都不做。我现在正在寻找几个小时,但我找不到解决方案。如果有人可以帮助我会很棒。 这是index.php:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/css/myCSSfile.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="/images/dit.ico"><link rel="stylesheet" href="/css/search.css">
<link rel="stylesheet" href="/css/button.css">
<link rel="stylesheet" href="/css/button2.css">
<script type="text/javascript" src="/js/resolutionfinder.js"></script>
<script type="text/javascript" src="/js/changeInputValue.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="/js/ajaxcalls.js"></script>
<body onload='ShowDivInCenter();' onresize='ShowDivInCenter();'>
<div class="cont">
<div id="main">
</script>
<div id="container">
<h1> UOP Directory Contents</h1>
<img class="logo" src="/images/logo.jpg" title="uop search engine" width="450" height="240" />
<form action=''>
<input type='search' id="search" name='s' autocomplete="off">
<input type='button' id="button" value='Search' class="button1" >
</form>
<div class="tfclear"></div>
</div>
<table class="sortable">
<thead>
<tr>
<th>Filename</th>
<th>Type</th>
<th>Size <small>(KB)</small></th>
<th>Date Modified</th>
</tr>
</thead>
<tbody>
<?php
// Opens directory
$rootDirectory=opendir(".");
starter($rootDirectory);
Function starter($myDirectory)
{
$testDir = 'C:\webdev\apache\htdocs\allo1';
// 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($dirArray[$index])/1024,2). ' KB';
// Gets Date Modified Data
$modtime=date("M j Y g:i A", filemtime($dirArray[$index]));
$timekey=date("YmdHis", filemtime($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 "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=folderSize($dirArray[$index]);
$size=number_format($size/1024,2). ' KB';
$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='#' onclick="call_php_function('./$namehref')">$name</a></td>
<td><a href='#' onclick="call_php_function('./$namehref')">$extn</a></td>
<td><a href='#' onclick="call_php_function('./$namehref')">$size</a></td>
<td sorttable_customkey='$timekey'><a href='#' onclick="call_php_function('./$namehref')">$modtime</a></td>
</tr>");
}
}
}
FUNCTION call_php_function(directory)
{
$.post("index.php", ("dir": directory), function(data){
alert(data); // Will output, whatever your starter-functions outputs
});
}
FUNCTION folderSize($dir)
{
$count_size = 0;
$count = 0;
$dir_array = scandir($dir);
foreach($dir_array as $key=>$filename){
if($filename!=".." && $filename!="."){
if(is_dir($dir."/".$filename)){
$new_foldersize = foldersize($dir."/".$filename);
$count_size = $count_size+ $new_foldersize;
}else if(is_file($dir."/".$filename)){
$count_size = $count_size + filesize($dir."/".$filename);
$count++;
}
}
}
return $count_size;
}
?>
<div id="ajaxresults" class="ajaxresults"></div>
<br />
<br />
<hr class="style-seven">
<br />
<br />
<div id="showing"></div>
<div id="results" class="results"></div>
</tbody>
</table>
<form><input type='button' id="button" value='DownLoadAll' class="button2"onClick="window.location.href='downloadZip.php?directtozip=$myDirectory&zipaki'"></form>
</div>
</div>
</body>
答案 0 :(得分:1)
请注意,此代码不适合公共生产用途。它足够好用它来学习。
HTML-Part:
<a href="#" onclick="call_php_function('any_php_function', 'anyattribut')">Directory-Name</a>
jQuery(Js)Part:
function call_php_function(php_function, anyattribut) {
$.post("functions.php", ("call_function": php_function, "attribute_name": anyattribut), function (data) {
// Data is what you get from the echo in the PHP-Script below
alert(data); // Outputs "hello"
}
}
PHP-Part:
function any_php_function($attribut) {
echo "hello";
}
$allowed_funcs = array("any_php_function", "any_other_function");
if (in_array($_POST["call_function"], $allowed_funcs))
call_user_func($_POST["attribute_name"]);
此代码足以满足您的目的:
HTML的部分:
<a href="#" onclick="call_php_function('directory-name')">directory-name</a>
jQuery的部分:
function call_php_function(directory) {
$.post("your_file.php", ("dir": directory), function(data){
alert(data); // Will output, whatever your starter-functions outputs
});
}
PHP-部分
if (isset($_POST["dir"]) && !empty($_POST["dir"])) {
starter($_POST["dir"]);
}