Solved

need to download file on server

Posted on 2011-03-07
9
227 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 108

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
 
LVL 108

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
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 prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now