MySQL Forums
Forum List  »  General

Re: EXAMPLE: How to store pictures in Mysql database
Posted by: James Zapico
Date: June 20, 2005 06:03PM

For the insert script:

$picture = fopen('path/to/file');
$data = fread($picture, filesize('path/to/file'));
$sqlpic = mysql_excape_string($data);

//It's good to find the mime type for later
//if the mime_magic extension is enabled
$mime = mime_content_type('path/to/file'))
//or on unix/linux
$mime = exec("file -bi path/to/file"

mysql_query("INSERT INTO `pictures` (`type`, `pic`) VALUES ('$mime', '$sqlpic')");

The display script should act like a picture:

$result =mysql_query("SELECT * FROM `picture` WHERE id = 1");

$picture = mysql_fetch_array($result);
header("Content-type: ". $picture['type']);
echo $picture['pic'];

That's it.

Options: ReplyQuote

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.