PHP saves pictures in MySQL database failed solution _php tips

Source: Internet
Author: User
Tags base64 fread phpmyadmin

This article analyzes the solution of PHP's failure to save pictures in MySQL database. Share to everyone for your reference. The specific analysis is as follows:

Picture Save database is not a sensible way, we mostly save the pictures to the server, and then put the image address to the database, so that each time we read the image address can be displayed, but below I would like to introduce a picture saved to the MySQL database problem-solving, the code is as follows:

Copy Code code 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 a error in your SQL syntax; Check the manual that corresponds to your MySQL server version for the right syntax to use near '? Shou Q? 仳!???? 1, >,mo? ' ^wz4in?? T Spring?????? U-ying/? ' at line 1

The code is as follows:

Copy Code code as follows:
$img = Fread ($fp, FileSize ($fileName));
$img = Addslashes ($img)

Continue to error, a variety of search, Baidu results are addslashes, or is addslashes also did not, really nonsense ah.

Copy Code code as follows:
Base64_decode
$img = Base64_encode ($img);

Insert success, picture file 17.0k, out for Base64_decode, show Normal, find a 16 method:

Copy Code code as follows:
$img = Bin2Hex ($img);

Effective, output do not need to decrypt, stored in the database is very large 25K, than Base64 also pit Dad, and then find, later, later, found that phpMyAdmin directly uploaded picture files can be used files than the base64 of the small, file 12.8k.

Turn phpMyAdmin source code, common.lib.php file 183 has a magical function, the code is as follows:

Copy Code code 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 (' \ ', ' \ n ', $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.8K as large as phpMyAdmin.

For example, the front desk image.html, the code is as follows:

Copy Code code as follows:
<title> Upload Pictures </title>

<body>
<form method= "POST" action= "upimage.php" enctype= "Multipart/form-data" >
<input type= "hidden" value= "204800" name= "max_file_size"/>
File: <input type= "file" name= "Imgfile"/>
<input type= "Submit" value= "OK" name= "submitbtn" style= "width:100px;height:23px"/></center>
</form>
</body>

The background processing upimage.php code is as follows:
Copy Code code as follows:
<?php
Insert a picture into the database
$imgfile =$_files[' Imgfile '];
$submitbtn =$_post[' submitbtn '];
if ($submitbtn = = ' OK ' and Is_array ($imgfile)) {
$name = $imgfile [' name ']; Get Picture Name
$type = $imgfile [' type ']; Get Picture Type
$size = $imgfile [' Size ']; Get picture length
$tmpfile = $imgfile [' Tmp_name ']; Picture upload on the path to the temp file
if ($tmpfile and Is_uploaded_file ($tmpfile)) {//To determine if the upload file is empty, file is not 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 a database statement, the picture data to be preceded by 0x, used to indicate the number of 16
if (mysql_query insert into images (name,type,image) VALUES ('. $name. "', '". $type. "', 0x". $imgdata. "))
echo <center> Insert Success! <br><br><a href= ' disimage.php ' > show Picture </a></center>;
Else
echo "<center> Insert failed! </center> ";
Mysql_close ();
}else
echo <center> Please select the picture First! <br><br><a href= ' image.html ' > Point this return </a></center> ";
} else
echo <center> Please select the picture First! <br><br><a href= ' image.html ' > Point this return </a></center> ";
?>

Display the picture disimage.php, the code is as follows:
Copy Code code as follows:
<?php
mysql_connect ("localhost", "root", "123456″");
mysql_select_db ("test");
Show the latest 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 file 12.8k as big as the original picture

Bin2Hex 16 Easy to use file 25K

Base64_encode easy to use, out of the file needs Base64_decode 17K

Addslashes is not good to use, continue to complain, note, in some Windows machine addslashes handy.

I hope this article will help you with your PHP program design.

Related Article

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.