Most people upload images by saving a path to the database, which is indeed fast during insertion and meets the characteristics of the web, but it is very troublesome to delete the file. You need to find and delete the file, this code can directly store the code into the database and delete it when it is deleted. Note: In this case, the database size will surge. Use the CREATETABLE-free 'upload
Most people upload images by saving a path to the database, which is indeed fast during insertion and meets the characteristics of the web, but it is very troublesome to delete the file. You need to find and delete the file, this code can directly store the code into the database and delete it when it is deleted. Note: In this case, the database size will surge. Use the create table without upload as appropriate.
Most people upload images by saving a path to the database, which is indeed fast during insertion and meets the characteristics of the web, but it is very troublesome to delete the file. You need to find and delete the file, this code can directly store the code into the database and delete it when it is deleted.
Please note: In this case, the database size will surge, please use it as appropriate <无>
CREATE TABLE `upload` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `type` varchar(20) NOT NULL, `data` mediumblob NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Post-Image
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);}
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();}