【问题标题】:php search issued filephp搜索发布的文件
【发布时间】:2016-04-14 08:06:33
【问题描述】:

我的脚本在类别中搜索文件,然后必须显示该文件。示例:X 用户正在 PCgames 类别中搜索 Y 游戏。 X 用户按下“搜索”按钮后,脚本必须显示在类别中找到的所有文件。但我在脚本中有一些问题 search.php 文件:

<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']])) //LINE 13
  $handle = opendir($categorydir[$_POST['category']]); //LINE 14
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){ //LINE 17
  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>

脚本给了我下一个错误:

注意:未定义索引:C:\xampp\htdocs\search.php 中的类别 第 13 行

注意:未定义的索引:在第 13 行的 C:\xampp\htdocs\search.php 中

注意:未定义索引:C:\xampp\htdocs\search.php 中的类别 第 14 行

注意:未定义索引:在第 14 行的 C:\xampp\htdocs\search.php 中

警告:scandir():目录名不能为空 C:\xampp\htdocs\search.php 在第 14 行目标目录不是 found 搜索结果:

警告:为 foreach() 提供的参数无效 C:\xampp\htdocs\search.php 第 17 行

一些帮助?

【问题讨论】:

    标签: php file-io search-engine displayobject


    【解决方案1】:

    您的类别似乎没有定义。您需要一个表单来创建类别。

    【讨论】:

    • 方法必须是post还是get?
    • 如果在同一个脚本中,则为 POST,否则为 GET
    【解决方案2】:

    将条件部分括在花括号 {} 中,如下所示:

    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']])) {
                $handle = opendir($categorydir[$_POST['category']]); //LINE 14
                $files = scandir($handle);
                echo 'Results of search:<br></br>';
                foreach($files as $file){ //LINE 17
                    echo($file);
                }
            } else {
                echo 'target directory not found';
        }
    }
    

    我在这里也做了一些其他的改进。

    【讨论】:

    • 这只是一个示例,可以为您指明正确的方向,请勿尝试复制/粘贴此代码。我没有测试过。
    猜你喜欢
    • 2011-04-16
    • 2012-05-06
    • 2013-05-30
    • 2012-03-10
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多