我有一个下拉列表,它将目录作为Windows文件夹收集在一起。用户选择值并单击“搜索”后,结果就是该目录中包含的PDF文件。事实证明,我找不到我的下拉列表结果为空的原因。我已经包含了我的代码以及空白下拉列表的屏幕截图。奇怪的是,下拉列表中只显示一条记录,这是一条快捷链接。请原谅我的无知,我是一个PHP新手,代码有点乱。我在IIS 7上运行该页面。
<?php // CMM_Tino.php
error_reporting(E_ALL);
/**
* SEARCH FOR PDF FILES BASED ON DIRECTORY NAME
*/
// ACTIVATE THIS TO SHOW THE REQUEST
// var_dump($_GET);
function getFileList($dir) {
// array to hold return value
$retval = array();
// add trailing slash if missing
if(substr($dir, -1) != DIRECTORY_SEPARATOR) $dir .= DIRECTORY_SEPARATOR;
// open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;
if(is_dir("$dir$entry")) {
$retval[] = array(
"name" => "$dir$entry" . DIRECTORY_SEPARATOR,
"type" => filetype("$dir$entry"),
"size" => 0,
"lastmod" => filemtime("$dir$entry") );
} elseif(is_readable("$dir$entry")) {
$retval[] = array(
"filename" => "$entry",
"filepath" => "$dir$entry"
);
}
}
$d->close();
return $retval;
}
// LINKS TO THE PDF FILES, IF ANY
$links = NULL;
// PUT THE PATH TO THE DIRECTORY OF FILE FOLDERS HERE
$path = './CMM_Search/CMM';
// GET THE LIST OF FILE FOLDERS
$folders = array_diff(scandir($path), array('.','..'));
// FILTER THE LIST OF FILE FOLDERS TO REMOVE EXTRANEOUS FILES
foreach ($folders as $key => $name)
{
if (!is_dir($path . DIRECTORY_SEPARATOR . $name)) unset($folders[$key]);
}
// IF THERE IS A REQUEST
$q = !empty($_GET['q']) ? $_GET['q'] : NULL;
if (!empty($q))
{
// IF THE REQUEST MATCHES ONE OF THE FILE FOLDERS
$folder = NULL;
foreach ($folders as $folder)
{
if (strpos($folder, $q) !== FALSE) break;
$folder = NULL;
}
if ($folder)
{
$links = "<p><b>$folder</b></p>" . PHP_EOL;
// GET THE LIST OF FILES IN THE FOLDER
$files = array_diff(scandir($path . DIRECTORY_SEPARATOR . $folder), array('.','..'));
// PREPARE LINKS TO THE PDF FILES
foreach ($files as $file)
{
$end = explode('.', $file);
$end = end($end);
$end = strtoupper($end);
if ($end != 'PDF') continue;
$links .= '<br>'
. '<a target="my_pdf" '
. 'href="'
. $path
. DIRECTORY_SEPARATOR
. $folder
. DIRECTORY_SEPARATOR
. $file
. '">'
. $file
. '</a>'
. PHP_EOL
;
}
}
else
{
$links = '<br>'
. 'Unknown Folder: '
. "<b>$q</b>"
. PHP_EOL
;
}
}
// CREATE THE SEARCH PAGE USING PHP HEREDOC NOTATION
$htm = <<<EOD
<!DOCTYPE html>
<html>
<head>
<title>Barfield CMM Lookup</title>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW" />
<!-- CSS styles for standard search box -->
<style type="text/css">
#tfheader{
background-color:#;
}
#tfnewsearch{
float:center;
padding:20px;
width: 1686px;
}
.tftextinput{
margin: 0;
padding: 5px 15px;
font-family: Arial, Helvetica, sans-serif;
font-size:14px;
border:1px solid #0076a3; border-right:0px;
border-top-left-radius: 5px 5px;
border-bottom-left-radius: 5px 5px;
}
.tfbutton {
margin: 0;
padding: 5px 15px;
font-family: Arial, Helvetica, sans-serif;
font-size:14px;
outline: none;
cursor: pointer;
text-align: center;
text-decoration: none;
color: #ffffff;
border: solid 1px #0076a3; border-right:0px;
background: #0095cd;
background: -webkit-gradient(linear, left top, left bottom, from(#00adee), to(#0078a5));
background: -moz-linear-gradient(top, #00adee, #0078a5);
border-top-right-radius: 5px 5px;
border-bottom-right-radius: 5px 5px;
}
.tfbutton:hover {
text-decoration: none;
background: #007ead;
background: -webkit-gradient(linear, left top, left bottom, from(#0095cc), to(#00678e));
background: -moz-linear-gradient(top, #0095cc, #00678e);
}
/* Fixes submit button height problem in Firefox */
.tfbutton::-moz-focus-inner {
border: 0;
}
.tfclear{
clear:both;
}
.auto-style1 {
width: 915px;
height: 90px;
margin-left: 0px;
}
.auto-style2 {
color: #0033CC;
}
</style>
</head>
<body>
<!-- HTML for SEARCH BAR -->
<div id="tfheader">
<br />
<img alt="Barfield" class="auto-style1" src="Barfield.jpg" /><br />
<!-- ************** NOTE THE $links VARIABLE WILL BE FILLED IN BY THE PHP SCRIPT ************** -->
$links
<br />
<!-- ************** NOTE THE ACTION ATTRIBUTE IS REMOVED TO CAUSE THIS SCRIPT TO SEND THE FORM REQUEST TO THE CURRENT URL ************** -->
<form id="tfnewsearch" method="get">
<span class="auto-style2"><strong>CMM</strong> </span>
<select class="tftextinput">
EOD;
define('ROOT', getenv("DOCUMENT_ROOT") . DIRECTORY_SEPARATOR);
$res = getFileList(ROOT . 'CMM_Search' . DIRECTORY_SEPARATOR . 'CMM' . DIRECTORY_SEPARATOR);
foreach ($res as $file){
$htm .="<option>" . $file['filename'] . "</option>";
}
$htm .= <<<EOD
</select>
<input type="submit" value="search" class="tfbutton">
</form>
<form id="tfnewsearch" method="get">
<span class="auto-style2"><strong>ADDN DOCUMENTS</strong> </span>
<input type="text" class="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
</form>
<form id="tfnewsearch" method="get">
<span class="auto-style2"><strong>PART NUMBER</strong> </span>
<input type="text" class="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
</form>
<img src='./QCJoe.jpg' width='200' height='200' /> Dev 1.1R1
EOD;
// WRITE THE HTML DOCUMENT TO THE BROWSER
echo $htm;