PHP-MYSQL

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.
Regards,
RGuillermoProject ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alain34Commented:
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++;
         }
      }

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
) TYPE=MyISAM;
;

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ldbkuttyCommented:
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.

<?php

if(VALID_USER) {

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 . '"' );

readfile('original.pdf');

}
else {
 echo "Invalid user";
 exit();
}
?>

For more, read:-
http://www.php.net/manual/en/function.header.php
0
ldbkuttyCommented:
replace the filenames accordingly instead of PDF.
0
alain34Commented:
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;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.