---
layout: post
title: 如何利用MySQL数据库制作一个图站
tags: [MySQL, 数据库, 图站, PHP]
---
最近白嫖了一个500GB的数据库,想想怎么利用一下?
# Talk is cheap,show me the code
数据库建表:
```sql
CREATE TABLE `FileUP` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Name` text NOT NULL,
`File` longblob NOT NULL,
`Size` int(11) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
```
PHP代码:
```php
query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()){
Header ( "Content-type: application/octet-stream" );
Header ( "Accept-Ranges: bytes" );
Header ( "Accept-Length: " . $row["Size"] );
Header ( "Content-Disposition: attachment; filename=" . $row["Name"] );
echo $row["File"];
}
}
else {
header('HTTP/1.1 404 NOT FOUND');
}
} else {
echo '
Mayx图床
Mayx图床
请选择需要上传的文件
';
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
$error = $_FILES['File']['error'];
$tmp_name = $_FILES['File']['tmp_name'];
$size = $_FILES['File']['size'];
$name = $_FILES['File']['name'];
print("\n");
if ($error == UPLOAD_ERR_OK && $size > 0) {
$fp = fopen($tmp_name, 'r');
$content = fread($fp, $size);
fclose($fp);
$content = addslashes($content);
$sql = "INSERT INTO `FileUP` (`Name`, `File`, `Size`)
VALUES ('".$name."', '".$content."', '".$size."');";
$con->query($sql);
$sql = "select @@identity;";
$result = $con->query($sql);
while($row = $result->fetch_assoc()) {
echo '上传完成,文件下载地址:'.$_SERVER['HTTP_X_FORWARDED_PROTO'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?ID=".$row['@@identity'].'';
}
} else {
echo "文件上传错误!";
}
}
echo '
By Mayx';
}
mysqli_close($con);
```
# 缺点
~~MySQL保存文件有限制,好像只能存16MB左右😓~~(改max_allowed_packet就可以了)
另外就是断点续传的问题,这个问题我回头再想一想吧。
# 演示(随时GG)
# 下一步改进的打算
~~想搞成切片上传,这样就没有文件大小上传的限制了。
不过这个好像得前端支持,先放个链接在这里吧:~~
不打算改进了……