• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3122
  • Last Modified:

php file_get_contents of pdf not working in IE

I am using php to view pdf files through the web browser.  The pdf file is stored outside of the root directory to deter direct linking (web page of pdf links requires password).  The link to the pdf file is as so:

<a href="caritas.php?file=priests/Caritas0809priests.pdf"> August 2009</a>


I am using ajax/jquery to open all pdf files in a new window.  

$(document).ready(function(){

    $("a[href$='.pdf'], a[href$='.doc'], a[href$='.xls']").click(function () {
           var features = "height=600,width=700,scrollTo,resizable=1,scrollbars=1,location=0";
            newwindow=window.open(this.href, 'Popup', features);
            return false;
      });



});
 
The pdf files open up in a new window in Firefox/Opera/Safari.  In IE, I just get a blank page where the source code is the following:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD>
<BODY></BODY></HTML>


--I  am able to view the PDF files in IE when I don't use the Javascript, that is, when the pdf is opened in the same window.  Is there something in my script that is causing this behavior in the IE browser?

Code to php file:
$file = $_GET['file'];
    $fileDir = '../../caritas/';

    if (file_exists($fileDir . $file))
    {
      
        $contents = file_get_contents($fileDir . $file);

       
        echo $contents;   
        
    }

Open in new window

0
libramak
Asked:
libramak
  • 5
  • 3
1 Solution
 
Timz358Commented:
As far I know isn't it that easy to display such a file.
I found this very quick by searching google and I tested it out, it works fine for me, it might helps you.
$file = $pdfName;
$path = "CreditReports/".$pdfName;
$fr = fopen($path, 'r');
$filedata = fread($fr, filesize($path));
fclose($fr);

header ( 'Content-Length: ' . filesize ( $path ) );
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=$file");
readfile($path);

Open in new window

0
 
gr8gonzoConsultantCommented:
You may be missing some HTTP headers - can you show us what caritas.php looks like?
0
 
libramakAuthor Commented:
Thanks for your response.

I don't want the users to get prompted to save or download the file.  

The page I attached has 2 links. . .
First Link: a pdf file is getting called via php (this one works in firefox/opera/safari)
Second Link: regular href link directly to file (works on all browsers)
 http://12.155.94.150/temp_pg.html

Please take a look at the page.  You can view the source to see the javascript code.  Below is the code used for the php page.

Thanks for your help!


<?
  $file = $_GET['file'];
    if (file_exists($file))
    { 
        $contents = file_get_contents($file);
        header ( 'Content-Length: ' . filesize ($file) );
        header('Content-type: application/pdf');

        echo $contents;
    }   ?>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
libramakAuthor Commented:
gr8gonzo:

I attached my code in the above post, the only difference I did from caritas,php and retrieve.php, is that in the retrieve.php,  I added the line (but didn't seem to make a difference):
header ( 'Content-Length: ' . filesize ($file) );

Open in new window

0
 
libramakAuthor Commented:
Have also added these lines (recommended from different sites found via google search), but also has not worked. ..
header("Pragma: public");
header("Cache-Control: max-age=0");

Open in new window

0
 
gr8gonzoConsultantCommented:
Hi libramak,

That code looks correct, although you may want to consider putting in some code to make sure nobody can simply access any file on the system by putting in the path in the "file" part of the URL. For example, try using str_replace to remove ".." from $_GET["file"] and make sure that you add a base directory to the beginning of it:

$file = "/path/to/base/directory/" . str_replace("..","",$_GET["file"];

That should give you some basic protection against people trying to access files outside of the "/path/to/base/directory" folder.

Now, regarding your script, there may not be a problem with the script itself. The headers seem correct. That said, I couldn't access the file even in Firefox. Try changing line 10:

    }   ?>

to

    }  
    else
    {
       echo $file . " does not exist in " . getcwd();
    }
 ?>
0
 
libramakAuthor Commented:
I updated the retrieve.php file (code below).  Here is the link to the page.

 http://12.155.94.150/temp_pg.html
<?
  $file = str_replace("..","",$_GET["file"]);
  
 if (file_exists($file))
    { 
        $contents = file_get_contents($file);
        header ( 'Content-Length: ' . filesize ($file) );
        header('Content-type: application/pdf');

        echo $contents;
      }  
    else
    {
       echo $file . " does not exist in " . getcwd();
    }   ?>  
  

Open in new window

0
 
gr8gonzoConsultantCommented:
I noticed that you're using a Xitami web server - do you know if it is doing anything special in terms of compressing content?
0
 
libramakAuthor Commented:
Looking through the Xitami admin configuration, there isn't anything about compressing content.  However, since you mentioned the webserver.  I tested the same code on 2 development computers, 1 machine has Apache, the other is Xitami.  The code worked successfully in all browsers when running under Apache.  I am getting the same problem with the code in IE under the Xitami development machine.  That being the case, I will get with the system administrator who is much more versed in Xitami than I.  .and will see if something can be done on his end.  Thanks for setting me on the right track!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now