修改错误php$upload_dir = uploads; 上传文件存储目录$db_file = dbtxt; 数据库文件路径$max_size = 2000 1024 1024; 上传文件最大限制2000MB$storage_limit = 400 1024 1024; 储存限制3000MB 检查上传目录是否存在不存在则创建if !file_exists$upload_dir
$max_size) {
echo '文件大小超过限制';
} else if ($free_size < 0) {
echo '已超量';
} else {
// 生成唯一ID
do {
$id = uniqid();
} while (file_exists($upload_dir . $id));
// 移动文件到上传目录
move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir . $id);
// 记录到数据库
$db = fopen($db_file, 'a+');
flock($db, LOCK_EX);
$existing_ids = array();
while ($line = fgets($db)) {
list($existing_id, $existing_filename) = explode("\t", trim($line));
$existing_ids[] = $existing_id;
if ($existing_filename == $filename) {
$filename = $existing_filename . '_' . uniqid();
}
}
if (in_array($id, $existing_ids)) {
$id .= '_' . uniqid();
}
fwrite($db, "$id\t$filename\n");
flock($db, LOCK_UN);
fclose($db);
}
} else {
echo '上传失败';
}
}
// 处理下载
if (isset($_GET['id'])) {
$id = $_GET['id'];
$db = fopen($db_file, 'r+');
flock($db, LOCK_SH);
$file_found = false;
while ($line = fgets($db)) {
list($file_id, $filename) = explode("\t", trim($line));
if ($file_id == $id) {
$file_found = true;
$filepath = $upload_dir . $file_id;
if (file_exists($filepath)) {
// 发送文件头,告诉浏览器要下载文件
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' . filesize($filepath));
// 发送文件内容
readfile($filepath);
// 删除文件
unlink($filepath);
// 更新数据库
fseek($db, 0);
ftruncate($db, 0);
$new_db = '';
while ($line = fgets($db)) {
list($existing_id, $existing_filename) = explode("\t", trim($line));
if ($existing_id != $id) {
$new_db .= $line;
}
}
fwrite($db, $new_db);
flock($db, LOCK_UN);
fclose($db);
exit;
} else {
echo '文件不存在';
}
}
}
if (!$file_found) {
echo '文件不存在';
}
flock($db, LOCK_UN);
fclose($db);
}
// 显示可下载的文件列表
$db = fopen($db_file, 'r');
flock($db, LOCK_SH);
$total_size = 0;
$available_files = array();
while ($line = fgets($db)) {
list($file_id, $filename) = explode("\t", trim($line));
$filepath = $upload_dir . $file_id;
if (file_exists($filepath)) {
$filesize = filesize($filepath);
$available_files[] = array(
'id' => $file_id,
'name' => $filename,
'size' => $filesize
);
$total_size += $filesize;
}
}
flock($db, LOCK_UN);
fclose($db);
$used_size = $total_size;
$free_size = $storage_limit - $used_size;
?
原文地址: https://www.cveoy.top/t/topic/iokM 著作权归作者所有。请勿转载和采集!