I am working on a project, where I am displaying files from a directory. When the user clicks on one of the row, then it calls a different PHP file, where a PDF file is downloaded, and other code is shown. Currently, when I click on the link, only the file is getting downloaded, and I cannot see the PHP page. What am I doing wrong?
use-case : The PDF downloaded is to be filled by the user and then uploaded again.. The code for upload and further processing is on send-mail.php. So, I need to download the file and send the user to send-mail.php as well. That's the use-case.
Original PHP from where redirect happens :
<?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 : When the PHP code below is present, I don't see the HTML code.
<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);
?>
Any ideas what I might be doing wrong? Thank you.
Update
Changed it to something like this :
<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>