note 107484 added to function.file-get-contents

From: Date: Sun, 12 Feb 2012 23:22:13 +0000
Subject: note 107484 added to function.file-get-contents
Groups: php.notes 
Request: Send a blank email to php-notes+get-186092@lists.php.net to get a copy of this message
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 '"/>'; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 202.153.80.106 ---- Manual Page -- http://www.php.net/manual/en/function.file-get-contents.php Edit -- https://master.php.net/note/edit/107484 Del: integrated -- https://master.php.net/note/delete/107484/integrated Del: useless -- https://master.php.net/note/delete/107484/useless Del: bad code -- https://master.php.net/note/delete/107484/bad+code Del: spam -- https://master.php.net/note/delete/107484/spam Del: non-english -- https://master.php.net/note/delete/107484/non-english Del: in docs -- https://master.php.net/note/delete/107484/in+docs Del: other reasons-- https://master.php.net/note/delete/107484 Reject -- https://master.php.net/note/reject/107484 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186092) next »