Php solution to saving images into mysql database failure

Source: Internet
Author: User
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 also summarized the related skills and provided some reference values. if you need them, I can refer to the examples in this article to analyze the solution to the failure of saving the images to the mysql database in php. 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:

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:

<? Php
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. continue to report the error, indicating that addslashes is easy to use on some windows machines.

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.