Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

need to download file on server

Posted on 2011-03-07
9
Medium Priority
?
237 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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 500 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 111

Assisted Solution

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

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 500 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

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

564 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