/    Sign up×
Community /Pin to ProfileBookmark

Filedownloading using PHP code

Hello Eveyrone ,
This is my first post in this form.

Actually i m storing files in mysql database using php code.I m able to store the files succesfully , but now I am listing all those files on a new php page and i want when any file is clicked , a download dialog box must open and it must allow us to save the files any where.

Lemme remind once again that files r stored in the database

It is urgent

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@sitehatcheryDec 16.2006 — [code=php]
$result=mysql_query('select file_name, file_title from files');
while($row=mysql_fetch_array($result)){
echo '<a href="path_to_file/'.$row['file_name'].'">'.$row['file_title'].'</a>';
}
[/code]


Whether you have the file stored in the database or you are storing it in a file and referencing it in the database, you still have to declare a path to the file to be reachable by your web users. This will either be declared within $row['file_name'] itself, or within the href attribute. I've never stored the file directly in the database myself in a blob field, so I'm not exactly sure how you would reference it in the href attribute. But the code structure would be the same.
Copy linkTweet thisAlerts:
@NightShift58Dec 16.2006 — [code=php]<?php

...
select content from table
...

$thisFILEname = $myDBfilename;
$thisFILEcontent = $myDBcontent;
header ("Content-Type: application/force-download");
header ("Content-Disposition: attachment; filename='$thisFILEname');
header ("Content-Transfer-Encoding: binary");
print $thisFILEcontent;
[/code]


Using the above method, you can essentially "force" a download of just about anything, WITHOUT having to first store it on disk and then linking to it.

This technique is usually used for displaying graphics that have been generated "on-the-fly" but can be used for any other content.

In the above example, the use of $thisFILEname and $thisFILEcontent are really superfluous. I put them in there so you can better follow the logic. The basic idea is that you're going to get a file name from your table, as well as the content, which is what the variables $myDBfilename and $myDBcontent stand for in the example.

Please note that I have a "<?php" at the beginning and nothing at the end of the script. That was not an oversight.
×

Success!

Help @prestonian spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...