douren9077 2018-09-26 12:23
浏览 56

在php中搜索栏结果疑难解答

I'm trying to build a code in php that will allow to me search (input filename given by the user) and display that file from my directory onto the website. Here is the code to display the downloadable pdf file:

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

I'm able to display the required search result. But the problem is that when the search bar is empty or null I want to display all the files from that directory which isn't happening. To be clear,

The desired search result

enter image description here

The result I want to get when the search bar value is null

enter image description here The result I get when the search bar is empty

enter image description here Can someone let me know where am I going wrong? Also I'm new to php.

  • 写回答

1条回答 默认 最新

  • duanri1985 2018-09-26 12:33
    关注

    replace

    if ($namehref == null)
    

    with

    if ($searchid == null)
    

    If thats not fixing the issue, show us more code

    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题