Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP MySQL query results auto download pdf files

Posted on 2008-10-07
4
Medium Priority
?
997 Views
Last Modified: 2013-12-12
I have a mysql table called download.  There are only 3 columms:  id, filename, down

As pdf files are created and saved in a folder on the server called /files this table is updated.  

The id is autoincrement
The filename is the filename of the pdf saved in the /files directory
The down column is initially set to 0, meaning that file has not been downloaded.

I have a PHP page that displays all of the filenames where down = 0, using the following query
"SELECT * FROM download WHERE downloaded = '0'"; and a repeating region for the table.

What I would like is to have a hyperlink that when clicked, automatically downloads all of the pdf files that meet the query, and then updates the table to make down = 1 for each of the files downloaded, so that they won't show up in the next page refresh.

I know how to do the update for the mysql table.


What I need help on is how to automatically download each pdf from the /files directory on the server to the clients computer that meets the down = 0 criteria
0
Comment
Question by:fastfind1
[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
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
AielloJ earned 2000 total points
ID: 22659327
You may want to consider a different method than a straight hyperlink for your files.  Using hyperlinks allows people to link directly to your PDF files, bypassing your site's web pages.  You can use the header code below to send the appropriate headers to the browser, then reading the file contents and relaying them to the browser.  Using this method your files can be in any directory on your site.  The URl of the file is never displayed to the user.

When you download the file you update the flag inthe table using the filename being downloaded.  I don't believe you can download multiple files at a time.  The only way I think you could do this would be to put all of the PDF files into a single .zip file and download it.  PHP has .zip file read/write libraries available:

  http://www.php.net/zip
<?  
$dir="/path/to/file/"; 
if (isset($_REQUEST["file"])) { 
    $file=$dir.$_REQUEST["file"]; 
    header("Content-type: application/force-download"); 
    header("Content-Transfer-Encoding: Binary"); 
    header("Content-length: ".filesize($file)); 
    header("Content-disposition: attachment; filename=\"".basename($file)."\""); 
    readfile("$file"); 
} else { 
    echo "No file selected"; 
} 
 ?> 

Open in new window

0
 

Author Comment

by:fastfind1
ID: 22659955
Actually I did know about the headers, but it only lets you download one pdf at a time.

Is there no way to download multiple pdf's at the same time, without zipping them first?
0
 
LVL 13

Expert Comment

by:AielloJ
ID: 22660232
Not that I'm aware of.  Maybe someone else knows how.  My understanding is that it's part of basic web security to get the user's OK for each file.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22660383
You can send multiple attachments in email (I know of no way to force multiple downloads) and if you get the client's email, it may be a useful piece of information some day!  Cheers, ~Ray
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
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…

670 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