【问题标题】:Search bar result troubleshooting in phpphp中的搜索栏结果故障排除
【发布时间】:2018-09-27 01:23:18
【问题描述】:

我正在尝试在 php 中构建一个代码,允许我搜索(用户提供的输入文件名)并将该文件从我的目录显示到网站上。以下是显示可下载 pdf 文件的代码:

<!doctype html>
<html>
<head>

<meta charset="UTF-8">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<title>Log Files</title>

<link rel="stylesheet" href="style.css">
<script src="sorttable.js"></script>
</head>

<body>
<div id="container">
<h1>Log Files</h1>

<table class="sortable">
    <thead>
    <tr>
        <th>Filename</th>
        <th>Type</th>
        <th>Size</th>
        <th>Date Modified</th>

    </tr>
    </thead>
    <tbody><?php

// Adds pretty filesizes
function pretty_filesize($file) {
    $size=filesize($file);
    if($size<1024){$size=$size." Bytes";}
    elseif(($size<1048576)&&($size>1023)){$size=round($size/1024, 1)." KB";}
    elseif(($size<1073741824)&&($size>1048575)){$size=round($size/1048576, 1)." MB";}
    else{$size=round($size/1073741824, 1)." GB";}
    return $size;
}

// Checks to see if veiwing hidden files is enabled
if($_SERVER['QUERY_STRING']=="hidden")
{$hide="";
 $ahref="./";
 $atext="Hide";}
else
{$hide=".";
 $ahref="./?hidden";
 $atext="Show";}

$directory = "downloads/";  // or example: $directory = "uploads/";
// opens this directory
$myDirectory = opendir($directory);


// Gets each entry
while($entryName=readdir($myDirectory)) {
   $dirArray[]=$entryName;
}

// Closes directory
closedir($myDirectory);

// Counts elements in array
$indexCount=count($dirArray);

// Sorts files
sort($dirArray);

// Loops through the array of files
for($index=0; $index < $indexCount; $index++) {

// Decides if hidden files should be displayed, based on query above.
    if(substr("$dirArray[$index]", 0, 1)!=$hide) {

// Resets Variables
    $favicon="";
    $class="file";


// Gets File Names
    $name=$dirArray[$index];
    $namehref=$dirArray[$index];

// Gets Date Modified
    $modtime=date("M j Y g:i A", filemtime($dirArray[$index]));
    $timekey=date("YmdHis", filemtime($dirArray[$index]));

// Separates directories, and performs operations on those directories
    if(is_dir($dirArray[$index]))
    {
            $extn="&lt;Directory&gt;";
            $size="&lt;Directory&gt;";
            $sizekey="0";
            $class="dir";

        // Gets favicon.ico, and displays it, only if it exists.
            if(file_exists("$namehref/favicon-32x32.png"))
                {
                    $favicon=" style='background-image:url($namehref/favicon-32x32.png);'";
                    $extn="&lt;Website&gt;";
                }

        // Cleans up . and .. directories
            if($name=="."){$name=". (Current Directory)"; $extn="&lt;System Dir&gt;"; $favicon=" style='background-image:url($namehref/.favicon-32x32.png);'";}
            if($name==".."){$name=".. (Parent Directory)"; $extn="&lt;System Dir&gt;";}
    }

// File-only operations
    else{
        // Gets file extension
        $extn=pathinfo($dirArray[$index], PATHINFO_EXTENSION);

        // Prettifies file type
        switch ($extn){
            case "png": $extn="PNG Image"; break;
            case "jpg": $extn="JPEG Image"; break;
            case "jpeg": $extn="JPEG Image"; break;
            case "svg": $extn="SVG Image"; break;
            case "gif": $extn="GIF Image"; break;
            case "ico": $extn="Windows Icon"; break;

            case "txt": $extn="Text File"; break;
            case "log": $extn="Log File"; break;
            case "htm": $extn="HTML File"; break;
            case "html": $extn="HTML File"; break;
            case "xhtml": $extn="HTML File"; break;
            case "shtml": $extn="HTML File"; break;
            case "php": $extn="PHP Script"; break;
            case "js": $extn="Javascript File"; break;
            case "css": $extn="Stylesheet"; break;

            case "pdf": $extn="PDF Document"; break;
            case "xls": $extn="Spreadsheet"; break;
            case "xlsx": $extn="Spreadsheet"; break;
            case "doc": $extn="Microsoft Word Document"; break;
            case "docx": $extn="Microsoft Word Document"; break;

            case "zip": $extn="ZIP Archive"; break;
            case "htaccess": $extn="Apache Config File"; break;
            case "exe": $extn="Windows Executable"; break;

            default: if($extn!=""){$extn=strtoupper($extn)." File";} else{$extn="Unknown";} break;
        }

        // Gets and cleans up file size
            $size=pretty_filesize($directory.$dirArray[$index]);
            $sizekey=filesize($directory.$dirArray[$index]);
    }
 $searchid = $_POST['search'];
 if($searchid == $namehref) {
         echo("<tr class='$class'>
        <td><a href='read.php?name_txt=$searchid'$favicon class='name'>$name</a></td>
            <td><a href='downloads/$searchid'>$extn</a></td>
        <td sorttable_customkey='$sizekey'><a href='downloads/$searchid'>$size</a></td>
        <td sorttable_customkey='$timekey'><a href='downloads/$searchid'>$modtime</a></td>
    </tr>");
        }
    }


else {
    if ($searchid == null) {
 echo("
    <tr class='$class'>
        <td><a href='read.php?name_txt=$namehref'$favicon class='name'>$name</a></td>
            <td><a href='downloads/$namehref'>$extn</a></td>
        <td sorttable_customkey='$sizekey'><a href='downloads/$namehref'>$size</a></td>
        <td sorttable_customkey='$timekey'><a href='downloads/$namehref'>$modtime</a></td>
    </tr>");
    }
  }
}
?>


    </tbody>
</table>

<h2><?php echo("<a href='$ahref'>$atext hidden files</a>"); ?></h2>
</div>
</body>
</html>

我能够显示所需的搜索结果。但问题是当search bar 为空或null 时,我想显示该目录中没有发生的所有文件。 说清楚,

想要的搜索结果

搜索栏值为空时我想要得到的结果

搜索栏为空时得到的结果

有人可以让我知道我哪里出错了吗?我也是 php 新手。

【问题讨论】:

    标签: php html ubuntu-16.04 lighttpd


    【解决方案1】:

    替换

    if ($namehref == null)
    

    if ($searchid == null)
    

    如果这不能解决问题,请向我们展示更多代码

    【讨论】:

    • 我做了必要的更改,它仍然显示相同的空白屏幕。我已经更新了问题中的完整代码。请检查一下并告诉我是否遗漏了什么
    • 尝试 isset($_POST['search']) 如果设置了参数 post 应该返回 false
    • 我做了isset($_POST['search'])。它确实做了一些改变。当搜索栏为空时,我得到所有文件的列表。但即使在搜索栏中传递了特定文件的名称后,也无法显示特定文件。
    • 要解决此类问题,最简单的方法是回显您的 $namehref 和您的 $searchid 并首先查看它们是否相等,如果存在大小写不匹配或类似的情况,只需使用其中一个这些功能来弥补这一点:dzone.com/articles/how-compare-strings-php
    • 字符串匹配..我得到了搜索结果..我想做的就是当搜索值为空时,我想显示目录中的所有文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多