Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PDF File Download from server error with file

Posted on 2014-01-31
6
Medium Priority
?
1,143 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 84

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 84

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 84

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

688 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