问题
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>
And the script gives me the next errors:
Notice: Undefined index: category in C:\xampp\htdocs\search.php on line 13
Notice: Undefined index: in C:\xampp\htdocs\search.php on line 13
Notice: Undefined index: category in C:\xampp\htdocs\search.php on line 14
Notice: Undefined index: in C:\xampp\htdocs\search.php on line 14
Warning: scandir(): Directory name cannot be empty in C:\xampp\htdocs\search.php on line 14 target directory not foundResults of search:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\search.php on line 17
Some help?
回答1:
Your category doesn't seem to be defined. You need a form to create the category.
回答2:
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.
来源:https://stackoverflow.com/questions/15122093/php-search-issued-file