我正在一个项目中,我正在其中显示目录中的文件。当用户单击某一行时,它将调用另一个PHP文件,并在其中下载PDF文件,并显示其他代码。当前,当我单击链接时,只有文件正在下载,而我看不到PHP页面。我在做什么错了?
用例:下载的PDF将由用户填写,然后再次上传。.上传和进一步处理的代码位于send-mail.php中。因此,我需要下载文件并将用户也发送到send-mail.php。那是用例。
发生重定向的原始PHP:
<?php
echo "<table align='center' class='loopblock'>";
echo "<tr>
<th>Existing templates</th>
</tr>";
$path = "/var/www/html/pdf/";
$files = scandir($path);
$files = array_diff(scandir($path), array('.', '..'));
foreach($files as $key){
echo"<tr class='loop'>
<td class='click' align='center' width='500' class='loop'><a class='loop' align='center'
href='/send-email.php?fileName=$key'>$key</a></td>
</a>
</tr>";
}
echo"</table>";
?>
send-email.php: 当下面的PHP代码存在时,我看不到HTML代码。
<div class="container">
<div class="contr"><h2>Drag and Drop your Tempalates to 'Drop Area' (size - under 10Mb)</h2></div>
<div class="upload_form_cont">
<div id="dropArea">Drop Area</div>
<div class="info">
<div>Files left: <span id="count">0</span></div>
<div>Destination url: <input id="url" value="/upload.php" readonly/></div>
<h2>Result:</h2>
<div id="result"></div>
<canvas width="500" height="20"></canvas>
</div>
</div>
</div>
<script src="js/script.js"></script>
<?php
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "/var/www/html/pdf/"; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['fileName']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
?>
任何想法我可能做错了什么?谢谢。
更新
将其更改为以下内容:
<div class="container">
<div class="contr"><h2>Drag and Drop your Tempalates to 'Drop Area' (size - under 10Mb)</h2></div>
<div class="upload_form_cont">
<div id="dropArea">Drop Area</div>
<div class="info">
<div>Files left: <span id="count">0</span></div>
<div>Destination url: <input id="url" value="/upload.php" readonly/></div>
<h2>Result:</h2>
<div id="result"></div>
<canvas width="500" height="20"></canvas>
</div>
</div>
</div>
<script src="js/script.js"></script>
<?php
session_start();
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
$path = "/var/www/html/pdf/"; // change the path to fit your websites document structure
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['fileName']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
break;
// add more headers for other content types here
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
break;
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
fclose ($fd);
exit();
}
?>
</body>
</html>
答案 0 :(得分:0)
我终于结束了添加此操作以解决问题:
<?php
$invite = $_GET['fileName'];
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=download_pdf.php?fileName=' . $invite . '">';
?>