?
Solved

need to download file on server

Posted on 2011-03-07
9
Medium Priority
?
233 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 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. 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 : Go t…
Suggested Courses

762 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