Solved

need to download file on server

Posted on 2011-03-07
9
228 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
9 Comments
 
LVL 109

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 109

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 109

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

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.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
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…

776 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