This article mainly introduces the PHP upload image into the database example, the need for friends can refer to the following
Most people's image upload is to save a path to the database, so that when the insertion is really fast, but also in line with the characteristics of the web, but in the removal of the trouble, need to find the file and delete, the code can be stored directly into the database, delete when deleted. Please note: This will increase the size of the database, please use table structure code as follows: CREATE table ' upload ' ( ' id ' int () unsigned not NULL Auto_increment, ' type ' varchar NOT NULL, ' data ' mediumblob not NULL, PRIMARY KEY (' id ') engine= InnoDB auto_increment=1 DEFAULT Charset=utf8; index.html code as follows: <!doctype html> <html> <head> &L t;title> post-image </title> </head> <body> <form Action = "post.php" method= "post" enctype= "Multipart/form-data" > <input type= "file" name= "file" id= "file"/ > <input type= "Submit" value= "OK"/> </form> </body> </html> POST.P HP Code is as follows: <?php if ($_files["file"] ["error"] > 0) { echo ' ERROR: '. $_files["File" ["Error"]. "<br/≫ ";} else { $type = $_files[' file ' [' type ']; $size = $_files[' file ' [' Size ']; $TMP =$_ files["File" ["Tmp_name"]; $FP = fopen ($tmp, ' RB '); $data = Bin2Hex (Fread ($fp, $size)); $DSN = ' mysql:host=localhost;dbname=test '; echo ' <pre> '; try{ $PDO = new PDO ($DSN, ' root ', ' root '); $PDO->ex EC ("INSERT into ' upload ' (' type ', ' data ') VALUES (' $type ', 0x$data)"); $id = $pdo->lastinsertid (); echo ' upload success!<a href= ' view.php?id= '. $id. ' " >View</a> '; $PDO = null; }catch (pdoexception $e) { echo $e->getmessage (); } &NBSP ; Echo ' </pre> '; fclose ($FP); The view.php code is as follows: <?php $id = $_get[' id ']; if (Is_numeric ($id)) { $DSN= ' mysql:host=localhost;dbname=test '; try{ $PDO = new PDO ($DSN, ' root ', ' root '); $RS = $pdo- >query (' select * from ' upload ' where ' id ' = '. $id); $row = $rs->fetchall (); $data = $row [0]; header ("content-type:${data[' Type ']}"); echo $data [' data ']; $PDO = null; }catch (pdoexception $e) { echo $e->getmessage (); }}else{  ; Exit (); }