Solved

PDF File Download from server error with file

Posted on 2014-01-31
6
1,006 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 83

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 83

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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 83

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 109

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

786 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