Posted on 2005-03-07
Medium Priority
Last Modified: 2012-06-21
Hi Experts,
I have many users accesing our site.
Each user has personal files.
We want to let each user ee only their files and let them download.
Any suggestions on how to let a file be downloaded.
Never done this before.
Question by:rguillermo
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
  • 2
LVL 16

Accepted Solution

alain34 earned 1500 total points
ID: 13475695
the best option that I come up with is to store files in a mysql database in blob format.
then it is easy to let user inly download their files!
to do the download, you can as follow:

   $dataStream = "";
   $query = "select chunk from download_chunk where id = $idDownload order by sequence";
   $result = mysql_query($query);  
   $number = mysql_numrows($result);
   $i = 0;
   if ($number != 0) {
      while ($i < $number) {  
         $dataStream .= mysql_result($result,$i,"chunk");

I'm storing file in chunk of 512K, because often mysql has a limit on the size of query.
This way my insert query is never longer than the limit defined by mysql.

CREATE TABLE download_chunk (
  id int(11) NOT NULL default '0',
  sequence int(11) NOT NULL default '0',
  chunk longblob NOT NULL

you just need to alter all that to link the id with your users and this way you can restrict download!
LVL 32

Expert Comment

ID: 13478166
Make a link to this PHP page when DOWNLOAD button is clicked.

For PDF:-
If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.



switch( $ext ){
   case "pdf": $ctype="application/pdf";              break;
   case "exe": $ctype="application/octet-stream";      break;
   case "zip": $ctype="application/zip";              break;
   case "doc": $ctype="application/msword";            break;
   case "xls": $ctype="application/vnd.ms-excel";      break;
   case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
   case "gif": $ctype="image/gif";                    break;
   case "png": $ctype="image/png";                    break;
   case "jpg": $ctype="image/jpg";                    break;
   default:    $ctype="application/force-download";

header('Content-type: $ctype');

header('Content-Disposition: attachment; filename="downloaded.' . $ext . '"' );


else {
 echo "Invalid user";

For more, read:-
LVL 32

Expert Comment

ID: 13478170
replace the filenames accordingly instead of PDF.
LVL 16

Expert Comment

ID: 13478196
I just realised my code was incomplete!

you should add the following for managing the download:

   $download_size = strlen($dataStream);
   header("pragma: public");
   header("expires: 0");
   header("Cache-Control: private");
   header("Content-type: application/octet-stream");
   header("Content-Disposition: attachment; filename=$fileDownload");
   header("Accept-Ranges: bytes");
   header("Content-Length: $download_size");
   echo $dataStream;

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
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.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 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