user3886556
user3886556

Reputation: 239

Prevent auto download PDF file using PHP

I have to show PDF file on my website so I have done coding using <iframe> but the issue is when I open my website, PDF file starts downloading instead of view. Here is my code

<?php
 $pdf = 'http://'. $_SERVER['SERVER_NAME']."/truwood/".$_REQUEST['pdf'];
 echo "<iframe src=\"$pdf\" width=\"100%\" style=\"height:600px\"></iframe>";
?>

NOTE: I have installed IDM (Internet Download Manager) to my laptop and I think it's the issue from IDM only.

Please suggest something that prevents auto download from any third party software in PHP.

Thanks in advance!

Upvotes: 0

Views: 3843

Answers (3)

Slimfresh
Slimfresh

Reputation: 13

I have a suggestion to your problem but its not tested. Copy the file to a temporary folder and change the file extension to something other than (.pdf) eg you can use (.pdf.xyz) You can then use the url of this file instead of the actual one. That way the download managers won't intercept the file and download it since they won't recognize the extension you added to the file. Your url will now read http://www.foo.bar/my_document.pdf.xyz OR http://www.foo.bar/my_document.xyz instead of http://www.foo.bar/my_document.pdf You can then create a cron job to delete any temporary files that are lets say 3 hours old.

Upvotes: 1

Deep Kakkar
Deep Kakkar

Reputation: 6297

You can choose one way in the following:

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");
@readfile('path\to\filename.pdf');

or: (note the escaped double-quotes). The same need to be use when assigning a name to it.

<?php

echo "<iframe src=\"file.pdf\" width=\"100%\" style=\"height:100%\"></iframe>";

?>

Note: There are known issues when trying to view PDF files in Windows 8. Installing Adobe Acrobat Reader is a better method to view these types of documents if no browser plug-ins are installed. and once try with disable IDM.

For more details, please check the SO Link.

Upvotes: 1

Ravi Roshan
Ravi Roshan

Reputation: 570

If your file is auto downloaded in IDM than its not your code issue. Try again after disable your IDM add on from browser.

Upvotes: 2

Related Questions