将html输入值转换为下载函数的$变量


Turning a html input value into a $variable for a download function

到目前为止,这个网站对我帮助很大,但我似乎终于遇到了一个问题。如何在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转换为变量?我以前读过的一个awnser是这样的:

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

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

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

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

在调用此代码的文件中,您将代码发布到:
(如果我理解良好: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)

这里有一个我编写并使用了一段时间的函数,请考虑为正确的contect类型添加一个例程:

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';
        }
}