到目前为止,这个网站对我帮助很大,但我似乎终于遇到了一个问题。 如何在 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)
关于php - 将 html 输入值转换为下载函数的 $variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17308098/