Hi,
I’m sorry, I needed to update other plugins. Now I’m back to you and can help you.
Please show your code so I can figure out why this is happening.
Best regards, Alex
Alex,
Here is the code.
$sql = “SELECT uploaded_file, uploaded_file_name FROM v36 WHERE chapter = $chapter”;
$result = @mysql_query($sql);
$data = @mysql_result($result, 0, “uploaded_file”);
$name = @mysql_result($result, 0, “uploaded_file_name”);
$filename=$name.”.pdf”;
header(“Content-type: application/pdf”);
header(“Content-Description: PHP Generated Data”);
header(“Content-Disposition: inline; filename=”.$filename);
echo $data;
I have tried alternative code with mysqli and mysqli_fetch_assoc(). I have tried changing headers as well. If I use the code in a normal php page, it runs fine, but if I embed it within a WP page, it tries to download the pdf, but the pdf is corrupted.
Thanks,
Chris
Hi,
Create a snippet that needs to be run everywhere. Look at my code below, it works fine. You can apply this example to your code.
This code will work for you if you got to link https://yoursite.test/?download_pdf=1
function wbcr_download_pdf() {
if(isset($_GET['download_pdf'])) {
$file_path = "tech-task.pdf";
if(!file_exists($file_path)) {
echo 'PDF file not found!';
exit;
}
header('Content-type: application/pdf');
header('Content-Description: PHP Generated Data');
header('Content-Disposition: attachment; filename='.$file_path);
// The PDF source is in original.pdf
readfile($file_path);
exit;
}
}
add_action('init', 'wbcr_download_pdf');
The most important thing that you need to consider is the right way to pdf files. My additional check will let you know if the path is correct or not.
if(!file_exists($file_path)) {
echo 'PDF file not found!';
exit;
}
Best regards, Alex
Alex,
I should have been clearer in my original post. The files are stored as longblobs in a MySQL database. They aren’t residing on the server.
The php script that I have used in the past retrieves the file using a query and then echoes it. The thing that is confusing is that it works fine outside of the WP context but returns a corrupted pdf in the WP context.
Thanks,
Chris
Hi, Chris
I can not verify your code, because I do not have a copy of your database.
You have provided too little information about your code.
Best regards, Alex