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 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 |
$img = Fread ($fp, FileSize ($fileName)); $img = Addslashes ($img) |
Continue with the error
All kinds of search, Baidu's 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 into the 16 system.
$img = Bin2Hex ($img);
Valid, the output is not decrypted. The database is very large 25K. than the base64 also pit dad.
Look again.
Later, later. found that phpMyAdmin directly uploaded picture files can be used in files smaller than base64. File 12.8k
Turn phpMyAdmin Source code
common.lib.php file 183 has 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 (' \ ', ' \ 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.
Cases
Front desk (image.html):
The code is as follows |
Copy Code |
<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>
|
Background processing (upimage.php):
The code is as follows |
Copy Code |
<?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> "; ?> |
Show Picture (disimage.php):
The code is as follows |
Copy Code |
<?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, continue to error (note: On some windows machine addslashes easy to use)