Picture saving database is not a good idea, we mostly save the picture to the server, and then save the image address to the database, so we can only read the image address every time to display, but below I would like to introduce a picture saved to the MySQL database problem resolution.
The code is as follows |
Copy Code |
Require ' class/db.php '; $fileName = "A1.jpg"; $fp = fopen ($fileName, "R"); $img = Fread ($fp, FileSize ($fileName)); Fclose ($FP); $db->execute ("Insert db2.testimg (' img ') VALUES (' $img ');"); |
Error
You have a error in your SQL syntax; Check the manual-corresponds to your MySQL server version for the right syntax-use-near '? Shou Q? Filming!???? 1, >,mo? ' ^wz4in?? T Chun?????? U-ying \? ' at line 1
The code is as follows |
Copy Code |
$img = Fread ($fp, FileSize ($fileName)); $img = Addslashes ($img) |
Continue to Error
All kinds of search, Baidu results are addslashes, or addslashes is not. That's ridiculous.
Base64_decode
$img = Base64_encode ($img);
Insert succeeded. Picture file 17.0k
Come out for Base64_decode, show normal
Find a way to get a 16 binary.
$img = Bin2Hex ($img);
Valid, output does not have to be decrypted. Deposit database is 25K large. than base64 still pit dad.
Find it again.
Later, later. Found phpMyAdmin directly uploaded image files can be used with a smaller file than Base64. File 12.8k
Turn phpMyAdmin Source code
common.lib.php file 183 There's a magical function.
The code is as follows |
Copy Code |
function pma_sqladdslashes ($a _string = ", $is _like = False, $crlf = False, $php _code = False) { if ($is _like) { $a _string = str_replace (' \ ', ' \\\\ ', $a _string); } else { $a _string = str_replace (' \ ', ' \ \ ', $a _string); }
if ($CRLF) { $a _string = Str_replace ("n", ' n ', $a _string); $a _string = Str_replace ("R", ' R ', $a _string); $a _string = Str_replace ("T", ' t ', $a _string); }
if ($php _code) { $a _string = Str_replace ("', ' \ '", $a _string); } else { $a _string = Str_replace ("", "", $a _string); }
return $a _string; }//End of the ' pma_sqladdslashes () ' function$img = Pma_sqladdslashes ($img); |
File size 12.8K is as large as phpmyadmin.
Cases
Front desk (image.html):
The code is as follows |
Copy Code |
Upload image
|
Background processing (upimage.php):
The code is as follows |
Copy Code |
Insert a picture into the database $imgfile =$_files[' Imgfile ']; $submitbtn =$_post[' submitbtn ']; if ($submitbtn = = ' OK ' and Is_array ($imgfile)) { $name = $imgfile [' name ']; Get the picture name $type = $imgfile [' type ']; Get Picture Type $size = $imgfile [' Size ']; Get picture length $tmpfile = $imgfile [' Tmp_name ']; The path to the temporary file on the image upload if ($tmpfile and Is_uploaded_file ($tmpfile)) {//Determine if the uploaded file is empty and the file is not an uploaded file Reading a picture stream $file =fopen ($tmpfile, "RB"); $imgdata =bin2hex (Fread ($file, $size)); Bin2Hex () converts binary data to hexadecimal representation Fclose ($file);
$mysqli =mysql_connect ("localhost", "root", "123456″"); Connect database functions mysql_select_db ("test"); Select Database Insert the database statement, before the picture data to add 0x, used to represent 16 binary number if (mysql_query ("INSERT into images (name,type,image) VALUES ('". $name. "', '". $type. "', 0x". $imgdata. ")") echo " Insert Success! Show pictures "; Else echo " Insert failed! "; Mysql_close (); }else echo " Please select the picture First! Click here to return "; } else echo " Please select the picture First! Click here to return "; ?> |
Show Picture (disimage.php):
The code is as follows |
Copy Code |
mysql_connect ("localhost", "root", "123456″"); mysql_select_db ("test"); Show the most recently inserted picture $result =mysql_query ("Select image from Images where id= (select Max (ID) from images)"); $row =mysql_fetch_object ($result); Header ("Content-type:image/pjpeg"); Echo $row->image; Mysql_close (); ?> |
Conclusion
Pma_sqladdslashes easy to use file 12.8k as large as the original picture
Bin2Hex 16 binary useful files 25K
Base64_encode easy to use, out of the file needs Base64_decode 17K
Addslashes bad use, continue to error (note: On some Windows machine addslashes useful)
http://www.bkjia.com/PHPjc/630680.html www.bkjia.com true http://www.bkjia.com/PHPjc/630680.html techarticle picture Saving database is not a good idea, we mostly save the picture to the server, and then save the image address to the database, so we just read the image address every time ...