【问题标题】:display a file on page using php使用 php 在页面上显示文件
【发布时间】:2013-02-27 19:31:21
【问题描述】:

我制作了一个在类别中搜索文件的脚本。我的问题是:搜索脚本后不显示搜索结果。示例:我正在搜索类似 :-? 的游戏交流3。搜索后脚本不显示任何结果。一些帮助?

<html>
<body>
<center>
<font color="black" size="4">
<?php
//define each directory here, the index starts with 0 == all and so on.
$categorydir = array('/Category/All/', '/Category/PCGames/', '/Category/Console/', '/Category/Movies/', '/Category/Music/', '/Category/XXX/', '/Category/Windows/', '/Category/Linux/', '/Category/Software/', '/Category/Documents/');
//if option selected and its valid number
if (isset($_POST['category']))
 if(ctype_digit($_POST['category']) && isset($categorydir[$_POST['category']]))
if(array_key_exists($_POST['category'], $categorydir) && is_dir($categorydir[$_POST['category']])){
is_dir($categorydir[$_POST['category']]);
  $files = scandir($categorydir[$_POST['category']]);
  echo 'target directory not found';
echo 'Results of search:<br></br>';
foreach($files as $file){
  echo($file);
}
?>
</font>
</center>
</body>
</html>
<html>
<head>
<title>DataHunters</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="header">
<h1 id="logo"><a href="index.html">DataHunters</a>
</h1>
</div>
<div id="navigation">
<ul>
<li><a class="active" href="index.html">Home</li></a>
<li><a href="chat.html">Chat</li></a>
<li><a href="contact.html">Contact</li></a>
<li><a href="http://www.forum.datahunters.ro">Forum</li></a>
</ul>
</li>
</div>
</body>
</html>

【问题讨论】:

  • 你期待什么输出?
  • 我期待在搜索后发布结果的脚本。
  • 您对结果有何期待?除了“找不到目标目录”和“搜索结果:”之外,您没有在上面的代码中输出任何内容:
  • 很难阅读你的代码。使用更多的大括号。
  • 所以,看这里。我想要的只是一个在类别中浏览文件然后显示该文件的脚本。我制作的脚本有一些错误,我期待你或其他任何人删除这些错误。(抱歉英语不好)

标签: php file search-engine displayobject


【解决方案1】:

我认为问题在于您正在打开一个目录并且没有对句柄做任何事情。您可以改为尝试使用 scandir() ,这将导致目录中的文件数组,而不是目录对象。

编辑:你可以试试这样的:

...
is_dir($categorydir[$_POST['category']])){
  $files = scandir($categorydir[$_POST['category']]);
}else{
  echo 'target directory not found';
}
echo 'Results of search:<br></br>';
foreach($files as $file){
  echo($file);
}

【讨论】:

  • 尝试将$handle = opendir($categorydir[$_POST['category']]); 替换为print_r(scandir($categorydir[$_POST['category']]));,看看这是否能让你朝着正确的方向前进。
  • 右边是“搜索结果:”,但没有我正在搜索的文件
  • 您是否得到了从 print_r() 打印出来的目录中的文件列表?如果是这样,而不是使用 print_r(),将 scandir() 的结果存储到一个数组中。代替 echo(),使用 foreach() 循环遍历数组并打印文件名。
  • 我不太了解 PHP 这个脚本是我在一些帮助下编写的。 :|。有什么清单?目录中所有文件的名称?
  • print_r() 是一个用于打印数组的 PHP 命令。您应该在生成的 HTML 中的某处看到该行执行了您目录中的文件列表,这表明 scandir() 已成功找到并读取目标的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 2015-12-15
相关资源
最近更新 更多