This article describes how to save images to the mysql database in php, and analyzes in detail the problems encountered when saving images to the database in php and the corresponding solutions, I have summarized the related skills for some reference.
This article describes how to save images to the mysql database in php, and analyzes in detail the problems encountered when saving images to the database in php and the corresponding solutions, I have summarized the related skills for some reference.
This article analyzes how php fails to save images to the mysql database. Share it with you for your reference. The specific analysis is as follows:
It is not wise to save images to the database. We usually save images to the server and then save the image address to the database so that we can display the image address every time we read the image address, the following describes how to save an image to the mysql database. The Code is as follows:
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:
The Code is as follows:
$ Img = fread ($ fp, filesize ($ fileName ));
$ Img = addslashes ($ img)
Continue to report the error. All the search results in Baidu are "addslashes", or "addslashes.
The Code is as follows:
Base64_decode
$ Img = base64_encode ($ img );
Inserted successfully. The image file is 17.0 kb and base64_decode is displayed. The displayed result is normal. Find a hexadecimal method:
The Code is as follows:
$ Img = bin2hex ($ img );
Valid. The output does not need to be decrypted. It is saved to the database at a size of 25 kb, which is more difficult than base64. Later, it was found that the image files directly uploaded by phpmyadmin can be smaller than base64, the file is 12.8 KB.
PHP MyAdmin source code. The common. lib. php file 183 has a magic function. The Code is as follows:
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.
For example, the Code for image.html is as follows:
The Code is as follows:
Upload images
The upimage. php code for background processing is as follows:
The Code is as follows:
<? Php
// 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 ";
?>
The following code displays the image disimage. php:
The Code is as follows: