Solved

need to download file on server

Posted on 2011-03-07
9
231 Views
Last Modified: 2012-05-11
I have a pdf file on my server.  if a php page is invoked, what is the php script to download that file from the file server?
0
Comment
Question by:COwebmaster
[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
9 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 35064953
You can link to the PDF file directly with a URL.  Or you can use a force-download script like this one.

HTH, and please post back with any questions, ~Ray
<?php // RAY_force_download.php
error_reporting(E_ALL);



// A FILE TO DOWNLOAD - THIS LINK COULD COME IN THE URL VIA $_GET, OR COULD BE GENERATED INSIDE THE SCRIPT
$url = "http://www.google.com/intl/en_ALL/images/logo.gif";

// USE CASE
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);

    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();

        // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
        ob_start();
        echo $filedata;
    }

    // ERROR
    else
    {
        die("ERROR: UNABLE TO OPEN $filename");
    }
}

Open in new window

0
 

Author Comment

by:COwebmaster
ID: 35065134
the user will need to fill out a form to download the pdf.  will the above form do that?
0
 
LVL 70

Assisted Solution

by:Jason C. Levine
Jason C. Levine earned 125 total points
ID: 35065315
No, but it's a trivial exercise to integrate.  You have the user fill out the form, process it with whatever script you typically use and then either add Ray's code to that script so the file downloads or redirect them to a new page that uses the script to force the download.

(no points for this answer please)
0
Independent Software Vendors: 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!

 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 35065717
When the user fills out a form the usual way of processing it would be to check the input data carefully and then use a session to carry the permissions needed to complete the download.  You can learn more about PHP forms here:
http://us3.php.net/tut.php

... and you can learn more about PHP sessions here (check the links in the left sidebar, too):
http://us3.php.net/manual/en/ref.session.php

Basically, you want to do the PHP function session_start() on every page, and then use the $_SESSION array to store and retrieve data.  The data in the $_SESSION array will persist across page loads.

You might want to get this excellent book that describes many of the things PHP and MySQL can do for you.  Now in its fourth printing, it has been a part of my professional library since Version One.
http://www.sitepoint.com/books/phpmysql4/

Best, ~Ray
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35065723
@jason1178: You deserve at least some points for the answer!  It makes perfect sense in the framework of what out Asker is trying to do.
0
 
LVL 3

Assisted Solution

by:pius_babbun
pius_babbun earned 125 total points
ID: 35065942
Check if this code helps you to download the pdf file.
$filename	= $_REQUEST['filename'];
	$filepath	= "foldername/".$filename;	
	$extenstion = explode(".",$filename);
 	
 
	$filesize = filesize($filepath);
	
 
	if($extension == "pdf")
	{
		header("Content-length: $filesize");
		header("Content-type: application/pdf");
	}
	
 	header("Content-Disposition: attachment; filename=$filename");

 	$handle 		= @fopen($filepath, "r");
	$contents 		= @fread($handle, filesize($filepath));
	
	fclose($handle);
	echo $contents;

Open in new window

0
 

Author Comment

by:COwebmaster
ID: 35132257
ok, I'll check that today.
0
 

Author Comment

by:COwebmaster
ID: 35159988
pius_babbun, where do I place the name of the file in that script?  Also, the file is located in this dir...

/download/Guide.pdf
0
 

Author Comment

by:COwebmaster
ID: 35160216
ok, got it , thx!
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migrating to phpbb forum from vBulletin 4.2 3 41
php subtract from date 2 30
CSS - Centering an image 2 28
How do I Check for duplicate entries in mysql 15 27
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

730 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