note 107484 deleted from function.file-get-contents by tularis
| From: | tularis@php.net | Date: | Mon, 13 Feb 2012 07:08:22 +0000 |
| Subject: | note 107484 deleted from function.file-get-contents by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-186095@lists.php.net to get a copy of this message | ||
Note Submitter: muhammadhu dot aiman at gmail dot com
----
here is a simple way to use file_get_contents to read binary files and to insert into a blob column
of the table.and to select the file contents that were written to database table to write to a
file/create and to display it.
<?php
/**
* insert process
**/
//name of the image to be loaded is 2.jpg
$image=mysql_real_escape_string("2.jpg");
//without mysql_real_escape_string insert into BLOB will generate error.
$file=mysql_real_escape_string(file_get_contents($image));
//connect to the server
$connect=mysql_connect("localhost","root","12345");
//select database
$sel=mysql_select_db("image",$connect);
//insert the file contents into column 'file'
$in=mysql_query("INSERT INTO images(name,file)
VALUES('$image','$file')");
/**
* content select from table process
**/
//select the image.
$im=mysql_query("SELECT * FROM images WHERE name='$image'");
while($row=mysql_fetch_array($im)){
/*
** file write process
*/
//the file contents that were written into the blob
$string=$row['file'];
//create a new file or open an existing file, to that we are going to write the binary contents
$file=fopen($image,'w+');
//create the image file by writting into file
$write=fwrite($file,$string);
//close file
$close=fclose($file);
}
//preview the image file
echo '<img src="';
echo $image;
echo '"/>';
?>