Link to home
Create AccountLog in
Avatar of RGuillermo
RGuillermoFlag for United States of America

asked on

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,
ASKER CERTIFIED SOLUTION
Avatar of alain34
alain34
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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
replace the filenames accordingly instead of PDF.
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;