Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I save files inside mysql and access them using php?

Status
Not open for further replies.

Sina

Technical User
Jan 2, 2001
309
CA
Dear gurus,

How can I save files inside Mysql?
i.e I have a couple of files (binary) of size 6 to 8 megs.
I'd like to save these files inside mysql database and use php to allow only selected users with correct users id and password to access the files (to download the files).
So basically I have a login script,
Users login, and they can download the files.

Is this possible at all?
If you could give me a hint or a sample script, I'd appricate it.

Thank you all
 
you can also make a table where you put the urls in of the file locations. depending on the user you make a query which file id's the user is allowed to see (which you can perhaps add to the user info)and maken hyperlinks to the physical file locations.


$list = mysql_num_rows($rs);
while($i < $list) {
$row = mysql_fetch_array($rs);
$urldescr=$row[&quot;urldescr&quot;];
$urlid=$row[&quot;urlid&quot;];
$urlsite=$row[&quot;urlsite&quot;];
$urlremarks=$row[&quot;urlremarks&quot;];
$totaal=$row[&quot;totaal&quot;];
print&quot;<td valign=top><img src=/images/thumb/$urlid-big.jpg border=0></td>&quot;;
print&quot;<td valign=top><a href=javascript:l2('$urlsite',$urlid)>$urldescr</a><br>$urlremarks</td>&quot;;
print&quot;<td valign=top><br><a href=whlist.php3?whname=$urldescr>$totaal</a></td>&quot;;
print&quot;</tr>&quot;;
$i++;
}
?>
 
All you have to do is store the binary data into a table (store the filename and the data), then when the user wants the file, print the appropriate file header and then the file's data after it... it works great!
Code:
/*
 user wants a the file &quot;test.pdf&quot; (a file you have in your table)
*/

$query = &quot;SELECT data FROM files WHERE filename='test.pdf'&quot;;
$results = MYSQL_QUERY($query);
if($file = MYSQL_FETCH_ASSOC($results))
{
 Header(&quot;Content-type: application/pdf&quot;);
 echo $file['data'];
}
else
{
 echo &quot;file not found!&quot;
}

i didn't test any of the above, but you get the idea, right? lemme know if you need more help :) -gerrygerry
Go To
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top