Solved

PDF File Download from server error with file

Posted on 2014-01-31
6
1,100 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

627 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