Most people's image upload is to save a path to the database, so in the insert is really fast, but also in line with the characteristics of the web, but it is very troublesome to delete, need to find the file and delete, the code can be directly stored in the database, deleted. Please note: In this case the database size will proliferate, please use it as appropriate
Table structure
Copy the Code code as follows:
CREATE TABLE ' upload ' (
' id ' int (ten) 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
Copy the Code code as follows:
<BR> post-image<br>
post.php
Copy the Code code as follows:
if ($_files["file" ["error"] > 0)
{
echo "Error:". $_files["File" ["Error"]. "
";
}
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 '
';
try{
$pdo = new PDO ($DSN, ' root ', ' root ');
$pdo->exec ("INSERT into ' upload ' (' type ', ' data ') VALUES (' $type ', 0x$data)");
$id = $pdo->lastinsertid ();
echo ' Upload success! View ';
$pdo = null;
}catch (Pdoexception $e) {
echo $e->getmessage ();
}
Echo '
';
Fclose ($FP);
}
view.php
Copy the Code code as follows:
$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 ();
}