2

到目前为止,这个网站对我很有帮助,但似乎我终于遇到了一个问题。如何在 html 输入中放置变量的问题之前已经被问过和回答过,但是有一个问题我似乎无法开始工作。

<?php
if(isset($_POST['file_name'])){
    $file = $_POST['file_name'];
    header('Content-Disposition: attachment; filename="'.$file.'"');
    readfile('uploads/'.$file);
    exit();
}
?>
<form action="force_download.php" method="post" name="downloadform">
  <input name= "file_name" type="hidden" value="test.txt" />
  <input type="submit" value="Download">
</form>

上面的代码用于下载文本文件“test.txt”,这是我上传文件夹中的许多文本文件之一。我的问题?如何将值 test.txt 转换为变量?我以前读过的一个遮阳篷是这样的:

<input type="hidden" name="file_name" value="<?php echo htmlspecialchars($file); ?>" />

问题是当我这样做时,它会下载 force_download.php 而不是我的任何文件。

我几天前才开始编码,所以是的......对于新手问题感到抱歉。

4

2 回答 2

0

这是我编写并使用了一段时间的函数,请考虑为正确的 contect-type 添加例程:

function downloadFile($fullPathToFile, $renameFile=''){
        if(is_file($fullPathToFile)){
            $path_parts = pathinfo($fullPathToFile);
            $dirname =  $path_parts['dirname'];
            $basename = $path_parts['basename'];
            $extension = $path_parts['extension'];
            $filename = $path_parts['filename'];
            $filesize = filesize($fullPathToFile);

            if(!empty($renameFile)){
                $extension = preg_match("/\.([^\.]+)$/", $renameFile, $matches);
                $extension = preg_replace('/\./', '', $matches[0]) ;
            } 
                    /*Change the following one line to type of files or comment out*/
            header("Content-Type: text/plain"); 
            header ("Content-Length: " . $filesize );
            header("Pragma: ");
            header("Cache-Control: ");
            if($renameFile){
                header("Content-Disposition: attachment; filename=\"$renameFile\"");
            }else{
                header("Content-Disposition: attachment; filename=\"$basename\"");
            }
            ob_clean();
            flush();
            readfile($dirname.'/'.$basename);
            die();
        }
        else {
            echo 'no such file or directory';
        }
}
于 2013-06-25T22:29:12.850 回答
0

这是一个建议,您可能需要做一些调整。

在调用此文件的文件中,您将代码发布到:(
如果我理解得很好:intranet.php)

<form action="force_download.php" method="post" name="downloadform">
<?php
$path = "./filesfolder/"; // this you need to change to your files folder

function createDir($path)
{   
    if ($handle = opendir($path)) 
    {
        while (false !== ($file = readdir($handle))) 
        {
            if (is_dir($path.$file) && $file != '.' && $file !='..')
                printSubDir($file, $path, $queue);
            else if ($file != '.' && $file !='..')
                $queue[] = $file;
        }
        printQueue($queue, $path);
    }
}
function printQueue($queue, $path)
{
    foreach ($queue as $file) 
    {
        printFile($file, $path);
    } 
}
function printFile($file, $path)
{
    if ($file=="thumbs.db") {echo "";}
    else 
    {
    echo "
<input name= \"".$file."\" type=\"hidden\" value=\"".$file."\" />";
    }
}
createDir($path);
?>
<input type="submit" value="Download">
</form>

这将为它找到的所有文件生成 html,并带有指向每个文件的可点击链接。在这种情况下,您不需要发布的文件中的 (我认为是 force_download.php)

于 2013-06-25T22:17:19.210 回答