Re: How to store an image into a mysql database using php language?
| From: | Richard Lynch | Date: | Wed, 24 Jul 2002 02:01:17 +0000 |
| Subject: | Re: How to store an image into a mysql database using php language? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-109217@lists.php.net to get a copy of this message | ||
>I want to store image data into mysql table using BLOB
Why?
It's just going to clog up your database, increase the risk of your database
self-destructing, and you can't *DO* anything with the data in that blob.
Unless you're actually de-constructing the image pixels with SQL and doing
photo comparisons by machine or something, the BLOB gains you nothing, and
risks a lot.
Just store the image in a highly-optimized, specialized database
specifically designed for large chunks of text, commonly known as a "file"
in the "OS" :-)
>but I don't know how
>to read it after. To record the image, I doesn't have any problem, but to
>read the file after and to view the image, I can't do anything the variable
>show only comment like this: memobin24.bin
>
>So how can I do?
>
>This was the script.
>
><?
>
>Include("connect.inc") ;
>
>$fp = fopen("image/photo.jpg","r") ;
>
>if ($fp) {
>
>$code2 = "" ;
>
>while (!feof($fp)) {
>
>$buffer = fgets($fp, 4096);
>
>$code2 = $code2.$buffer;
>
>}
>
>}
>
>$code2 = addslashes($code2) ;
>
>$table = 'image' ;
>
>$instruction = mysql_query("INSERT into $table values ('','fichier
>inage','$code2','') or die("FATAL ERROR") ;
>
>?>
You'll need to have *TWO* files:
File 1: index.php
<HTML><BODY><IMG
SRC=image.php/photo.jpg?filename=photo.jpg></BODY></HTML>
File 2: image.php
<?php
$filename = $_GET['filename']; # Comment out for older versions of PHP.
$query = "select nameofblobfieldgoeshere from image where
somethinggoeshere = '$filename'";
$data = mysql_query($query) or error_log(mysql_error());
$image = mysql_result($data, 0, 0);
$len = strlen($image);
header("Content-type: image/jpg");
header("Content-length: $len");
echo $image;
?>
This will work -- It's just silly[*] to use MySQL BLOBs to store images.
[*] In very high-traffic servers where you have *MAYBE* done some serious
performance testing, you *MIGHT* be able to prove that mysql_connect is
faster than a file access. Or, if you don't know where the file is located
without firing up the database anyway, *MAYBE* a second query and getting a
BLOB will be faster. But you've *GOT* to be very desperate for CPU cycles
for this to be relevant.
--
Like Music? http://l-i-e.com/artists.htm