An error occurred while saving the php image to the mysql database. It is not wise to save images to the database. we generally save images to the server and then save the image addresses to the database, in this way, it is not wise to read the image address and save the image to the database. most of us save the image to the server and then save the image address to the database, in this way, we can display the image address every time we read the image address, but next I will introduce the solution to saving an image to the mysql database.
The code is as follows: |
|
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 an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''? Qaq? Success !???? 1,>, Mo? '^ WZ4in ?? T spring ?????? U? Role \? 'At line 1
The code is as follows: |
|
$ Img = fread ($ fp, filesize ($ fileName )); $ Img = addslashes ($ img) |
Continue error reporting
All the search results in Baidu are "addslashes", or "addslashes. It's really nonsense.
Base64_decode
$ Img = base64_encode ($ img );
Inserted successfully. Image file 17.0 KB
Base64_decode
Find a hexadecimal method
$ Img = bin2hex ($ img );
Valid, and the output does not need to be decrypted. The database size is 25 kB. It's worse than base64.
Try again.
Later, later. It is found that the image files directly uploaded by phpmyadmin can be smaller than base64. File 12.8 KB
Phpmyadmin source code
The common. lib. php file 183 has a magical function.
The code is as follows: |
|
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 ); |
The file size is 12.8 KB, which is the same as that of phpmyadmin.
Example
Foreground (image.html ):
The code is as follows: |
|
Upload images
|
Background processing (upimage. php ):
The code is as follows: |
|
// Insert an image into the database $ Imgfile = $ _ FILES ['imgfile']; $ Submitbtn = $ _ POST ['submitbtn ']; If ($ submitbtn = 'OK' and is_array ($ imgfile )){ $ Name = $ imgfile ['name']; // Obtain the image name $ Type = $ imgfile ['type']; // Obtain the image type $ Size = $ imgfile ['size']; // get the image length $ Tmpfile = $ imgfile ['tmp _ name']; // path of the temporary file uploaded to the image If ($ tmpfile and is_uploaded_file ($ tmpfile) {// determines whether the uploaded file is empty or not // Read the image stream $ File = fopen ($ tmpfile, "rb "); $ Imgdata = bin2hex (fread ($ file, $ size); // bin2hex () converts binary data to a hexadecimal representation. Fclose ($ file ); $ Mysqli = mysql_connect ("localhost", "root", "123456"); // connect to the database function Mysql_select_db ("test"); // select a database // Insert a database statement. add 0x before the image data to indicate the hexadecimal number. If (mysql_query ("insert into images (name, type, image) values ('". $ name. "','". $ type. "', 0x ". $ imgdata. ")")) Echo" Inserted successfully! Show image "; Else Echo" Insertion failed! "; Mysql_close (); } Else Echo" Select an image first! Click here to return "; } Else Echo" Select an image first! Click here to return "; ?> |
Display Image (disimage. php ):
The code is as follows: |
|
Mysql_connect ("localhost", "root", "123456 ″); Mysql_select_db ("test "); // Display the latest inserted image $ 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.8 k is as big as the original image
Bin2hex hexadecimal easy-to-use file 25 K
Base64_encode is easy to use. the output file requires base64_decode 17 K.
Addslashes is not easy to use and an error is reported (note: addslashes is easy to use on some windows machines)
...