i can upload a file in the database using sql but how can i make a download link for it? like when you download something online then a message box will come up you will be asked if you would like to open it with a program or save it. how can i do that in php? can you give me the codes for it? i'm still a noob.
+7
A:
Place this code on a page (along with the PHP code to get the info from the DB and place it in the variables for the name/size/data, then link to that page.
<?php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $name_of_file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);
echo $file_data;
?>
Not all of the headers listed above are strictly necessary - in fact, only the Content-Type header is truly necessary to make the download work properly. The Content-Disposition header is good to include so that you can specify a proper filename; the others just help the browser handled the download better and can be omitted if you desire.
Amber
2009-08-02 06:19:11
How can i download a file directly from the database using that? Users upload a file in the database and when the administrator needs to check that file of course he needs to download it. How can i do that?
2009-08-02 06:34:51
If you don't have enough grasp of PHP and DB concepts to use the above code to accomplish what you're trying to do, you might want to go with a premade database management solution like PHPMyAdmin (http://www.phpmyadmin.net/) instead.
Amber
2009-08-02 06:51:27
Thanks. I will go there.One more thing, when I store a file as a blob in the database, do I have to store in a separate column the file name and the file size or it isn't necessary?I know this might seem like a stupid question but I'm really a noob. Thanks anyway.
2009-08-02 07:26:51
@phpnoob perhaps this will help: the variables $file_data and $name_of_file draw their values from the database. Dav has omitted the parts of code where this is being done, for the sake of brevity.
Here Be Wolves
2009-08-02 07:36:24
@phpnoob - you would need to store the file name separately for certain, as it is unrelated to the data of the file (what is stored in the actual BLOB). You could probably calculate the file size by simply looking at the size of the BLOB data in the query result.
Amber
2009-08-02 09:43:35