Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1186
  • Last Modified:

PDF File Download from server error with file

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
Sevron
Asked:
Sevron
  • 3
  • 2
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
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
 
SevronAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
Industry Leaders: 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!

 
SevronAuthor Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
Ray PaseurCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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