layout: post title: 如何利用MySQL数据库制作一个图站
最近白嫖了一个500GB的数据库,想想怎么利用一下?<!--more-->
数据库建表:
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
$con=mysqli_connect("数据库地址","用户名","密码","数据库名");
if (mysqli_connect_errno($con))
{
die("连接 MySQL 失败: " . mysqli_connect_error());
}
if ( $_GET[ID] != '' ) {
header('Access-Control-Allow-Origin: *');
$sql = "SELECT * FROM `FileUP` WHERE `ID` = '".addslashes($_GET[ID])."' ";
$result = $con->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 '<title>Mayx图床</title>
<h1>Mayx图床</h1><hr>
请选择需要上传的文件
<form enctype="multipart/form-data" method="post" action="">
<input type="file" name="File" />
<input type="submit" name="submit" value="submit" />
</form>
<br />';
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 '上传完成,文件下载地址:<a href="//'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?ID=".$row['@@identity'].'" >'.$_SERVER['HTTP_X_FORWARDED_PROTO'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?ID=".$row['@@identity'].'</a>';
}
} else {
echo "文件上传错误!";
}
}
echo '<hr />
<center><a href="https://mabbs.github.io/">By Mayx</a></center>';
}
mysqli_close($con);
MySQL保存文件有限制,好像只能存16MB左右😓(改max_allowed_packet就可以了)
另外就是断点续传的问题,这个问题我回头再想一想吧。
https://mayx.leanapp.cn/up.php
想搞成切片上传,这样就没有文件大小上传的限制了。
不过这个好像得前端支持,先放个链接在这里吧:https://github.com/fex-team/webuploader
不打算改进了……