Linuxマシンの/tmpディレクトリ内のファイルを検索するHTMLテキストボックスを表示する単一のPHPスクリプトを作成するのに問題があります。ユーザーがテキストボックスに文字列を入力して送信ボタンを押すと、一致するハイパーリンクがテキストボックスの下に表示され、Webディレクトリ(/ tmp内)外のファイルの名前が表示されます。
ハイパーリンクをクリックすると、/tmpディレクトリからファイルをダウンロードしたいと思います。残念ながら、単一のphpスクリプトを使用する必要があるため、phpヘッダーコマンドを使用しているため、HTMLコンテンツがファイルに追加されています。
phpページのhtmlをファイルに追加せずに、また複数のphpスクリプトを使用せずに、ファイルをダウンロードする方法はありますか?
<?php
function displayfiles()
{
echo '<ul>';
if ($handle = opendir('/tmp'))
{
while ($file = readdir($handle))
{
if ($file != "." && $file != ".." && (strstr($file, $_POST['searchbox'])))
{
echo '<li><a href="search.php?file='.$file.'">'.$file.'</a></li>';
}
else if ($file != "." && $file != ".." && (eregi($file, $_POST['searchbox'])))
{
echo '<li><a href="search.php?file='.$file.'">'.$file.'</a></li>';
}
}
closedir($handle);
}
echo '</ul>';
}
function downloadfile($filename)
{
header("Content-disposition: attachment; file='/tmp/$filename' filename=$filename");
header("Content-Length: " . filesize("/tmp/$filename"));
header("Pragma: no-cache");
header("Expires: 0");
readfile("/tmp/$filename");
exit();
}
echo
"<html>
<head>
<title>File Search</title>
</head>
<body>
<form method='POST'>
<input id='searchbox' name='searchbox' id='searchbox' type='textbox' />
<input type='submit' name='submit' />
</form>";
if (isset($_POST['submit']))
{
displayfiles();
}
else if (isset($_GET['file']) && file_exists("/tmp/".$_GET['file']))
{
downloadfile($_GET['file']);
}
echo "</body></html>";
?>