Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

PHP-MYSQL

Posted on 2005-03-07
4
Medium Priority
?
177 Views
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.
Regards,
0
Comment
Question by:RGuillermo
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

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

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
 
LVL 32

Expert Comment

by:ldbkutty
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.

<?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
 
LVL 32

Expert Comment

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

Expert Comment

by:alain34
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;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
By following these Magento e-commerce development tips, you can increase your website's conversion and profitability. Read this post for more details.
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

580 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