-2

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>";
?>
4

1 に答える 1

0

ファイルの出力を処理する必要があります-他の何かを出力する前にダウンロードしてください、迅速で汚い例(コメントで提案されているように2つではなく1つの関数だけでも、あなたはアイデアを得ると思います):

<?php

function action_download() {
    if (!isset($_GET['file'])) return FALSE;
    $file = sprintf('/tmp/%s', $_GET['file']);
    $file = realpath($file);
    if (substr($file, 0, 5) !== '/tmp/') return FALSE;
    if (!is_file($file) || !is_readable($file)) return FALSE;

    header("Content-disposition: attachment; filename='".basename($file)."'");
    header("Content-Length: " . filesize($file));
    header("Pragma: no-cache");
    header("Expires: 0");
    readfile($file);
    return TRUE;
}

if (action_download()) return;

?>
<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>
<?php
                if (isset($_POST['submit']))
                {
                        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>';
                }
?>
</body></html>

/tmp/また、アプリケーション、そのユーザー、さらにはシステムを攻撃するために使用される可能性があるため、ダウンロード用に提供されるべきではないデータが内部に存在する可能性があることに注意してください。

于 2012-09-22T17:59:34.007 に答える