Re: Displaying files from database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mar 28, 2007, at 1:36 PM, Richard Davey wrote:

Philip Thompson wrote:

Hi. I'm storing an uploaded file into a MySQL database. I want the file to then be downloaded and viewed. Uploading looks like:

Assuming you actually have a good reason *why* you are storing uploaded files in your database, how has the table been set-up? What is the table type and the column you are storing this file in? Is PHP set to automatically magic quote the data on insert? If so, you're in for a world of pain, so disable it.

Storing in the database to keep the data all together. Don't want to deal with storing it elsewhere and keeping up with it. It's a first for me to do it this way. The column is type LONGBLOB.


if (is_uploaded_file($file) && $filename) {
  $handle = fopen ($file, 'r');

You should always fopen with 'rb' for binary safe, system-portable handling.

I actually had it that way. When I gave the example, I had tested it both ways to see if that was where the problem was occurring. It's back to 'rb'.


Downloading looks like:
<snippet>
$app = applications ($_GET["id"]);
header ("Status: 200 OK");
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header ("Content-Length: ".$app["size"][0]);
header ("Content-Type: ".$app["type"][0]);
header ("Content-Disposition: attachment; filename=Resume-".$app ["full"][0]);
echo $app["resume"][0];
exit;
</snippet>
What am I doing wrong?!! =D Thanks in advance.

What are the size of these files? First of all, you need to base DECODE your data before sending to the client. Secondly, check if SQL is automatically escaping the data, which will cause you no end of trouble.

Third, if your files are larger than the maximum amount of memory a single PHP script can use on your server, your way of doing this will fail, because you are echoing out the data.

I limit the upload size, so I know that it *should* never reach the limit.


Actually storing the files as files, and then using passthru() to send them to the browser avoids this limitation. As it stands, if there is a 4MB file, each call to your script for it will use 4MB of memory minimum, so I hope you've got a dogs-bollocks server there, or a pitifully low traffic site ;)

Cheers,

Rich


~Philip

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux