Solved

PDF File Download from server error with file

Posted on 2014-01-31
6
955 Views
Last Modified: 2014-03-26
Good Morning,

Currently having an issue whilst trying to download a PDF file from our live server.

When trying to view the file in Adobe Reader I get the following error :-

'Adobe Reader could not open 'giraffes.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).'

The PDF file opens correctly and has no issues when I open it on the live server so I know its not a problem with the actual document. I assume its a problem with how I'm calling it in PHP.

The PHP code I'm using is

<?php

include("../../../res/includes/config.php");

$pkAttachmentID = $_GET['id'];

$link = sqlsrv_connect($sName, $cInfo);

$sql = "SELECT FileName, Title FROM tblAttachments WHERE pkAttachmentID = " . $pkAttachmentID;

$result = sqlsrv_query($link, $sql);

if ($result === false) {
    die(print_r(sqlsrv_errors(), true));
}

$row = sqlsrv_fetch_array($result);

$FileName = $row['FileName'];
$title = $row['Title'];

sqlsrv_close($link);

header("Content-disposition: attachment; filename=".$title.".pdf");
header("Content-type: application/pdf");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

readfile('C:\inetpub\wwwroot\File_Store\{'.$FileName.'}.pdf');

exit;
?>

Open in new window


I've also tried saving the file rather than directly opening it but I get the same error.

I think thats about as much information I can give but if I've missed anything important please let me know and I'll get back to you ASAP

Thanks for your time
0
Comment
Question by:Sevron
  • 3
  • 2
6 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39823445
What is the size of the downloaded file?  Does it match the original?  
I wonder about your link to the file and why there would be the '{}' in there.
// without {}
readfile('C:\inetpub\wwwroot\File_Store\'.$FileName.'.pdf');

//or
readfile('File_Store/'.$FileName.'.pdf');

Open in new window

http://us1.php.net/manual/en/function.readfile.php
0
 

Author Comment

by:Sevron
ID: 39823474
Hi Dave,

Thanks for the quick response.

I've checked the file sizes. The PDF on the server is 36kb, the downloaded PDF is showing as either 0kb or 1kb, seems to differ each time.

I've tried altering the file path to your suggestions above but I still get the same error.

Thanks
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39823573
Try the example on this page http://us1.php.net/manual/en/function.readfile.php with one of your files and see what happens.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Sevron
ID: 39823606
Hi,

I've used that example, the download popup is showing the correct size but when it opens or I save it its still showing as 1kb.

Is it possible its another issue rather than with the code I'm using to call it?

Thanks again
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39824646
It might be.  I suggest you look at the downloaded file with a text editor to see if maybe it's just an error message.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39826200
Not specific to PDF, but I know that this script will force a download on every browser I've tested.

<?php // RAY_force_download.php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('log_errors',     TRUE);


// DEMONSTRATE HOW TO CAUSE A FILE DOWNLOAD
// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28353087.html


// REQUIRED FOR USE WITH THE PHP date() FUNCTIONS
date_default_timezone_set('America/New_York');

// A FILE TO DOWNLOAD - THIS LINK COULD COME IN THE URL VIA $_GET, OR COULD BE GENERATED INSIDE THE SCRIPT
$url = "http://www.LAPRBass.com/RAY_short_text_file.txt";

// THE USE CASE FOR THE FUNCTION
force_download($url);


// FUNCTION TO FORCE A DOWNLOAD FROM A FILE
function force_download($filename)
{
    // GET THE CONTENTS OF THE FILE
    $filedata = @file_get_contents($filename);

    // SUCCESS
    if ($filedata)
    {
        // GET A NAME FOR THE FILE
        $basename = basename($filename);

        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$basename");
        header("Content-length: ".(string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // WRITE THE FILE
        echo $filedata;
    }

    // ERROR
    else
    {
        trigger_error("ERROR: UNABLE TO OPEN $filename", E_USER_ERROR);
    }
}

Open in new window

HTH, ~Ray
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now