PHP搜索发布的文件 [英] php search issued file

查看:93
本文介绍了PHP搜索发布的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My script search in a category for a file and then must display the file. Example: the X user is searching in category PCgames for Y game. After the X user push the "Search" button script must display all the files found in category. But I have some issues in the script search.php file:

<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行

Notice: Undefined index: category in C:\xampp\htdocs\search.php on line 13

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

Notice: Undefined index: in C:\xampp\htdocs\search.php on line 13

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

Notice: Undefined index: category in C:\xampp\htdocs\search.php on line 14

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

Notice: Undefined index: in C:\xampp\htdocs\search.php on line 14

警告:scandir():目录名称不能为空 第14行的C:\ xampp \ htdocs \ search.php目标目录不正确 founds搜索结果:

Warning: scandir(): Directory name cannot be empty in C:\xampp\htdocs\search.php on line 14 target directory not foundResults of search:

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

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\search.php on line 17

有帮助吗?

推荐答案

将条件部分用大括号{}括起来,如下所示:

Enclose the conditional parts in curly braces {} like so:

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';
    }
}

我在这里也做了其他改进.

I've made some other improvements here as well.

这篇关于PHP搜索发布的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆