更新 index.php
可以不依赖任何扩展的小而美的PHP图床程序。
This commit is contained in:
parent
cfa8f0a48b
commit
da1dfd7312
122
index.php
122
index.php
@ -1,21 +1,6 @@
|
||||
<?php
|
||||
$base_upload_dir = __DIR__ . '/o/'; // 如存放在当前程序目录下直接/ 即可如果其他目录前后都要/包含,如:/s/
|
||||
|
||||
$max_filename_length = 180; // 最大文件名长度
|
||||
|
||||
// 获取服务器的最大上传文件大小和内存限制
|
||||
$max_upload_size = ini_get('upload_max_filesize');
|
||||
$max_memory_limit = ini_get('memory_limit');
|
||||
|
||||
// 生成随机文件名
|
||||
function generateRandomFileName($length = 10) {
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$randomString = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, strlen($characters) - 1)];
|
||||
}
|
||||
return $randomString;
|
||||
}
|
||||
// 如存放在当前目录下直接/即可,如果选择其他目录前后都要/包含,如:/o/
|
||||
$base_dir='/o/';
|
||||
|
||||
// 处理文件上传
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
|
||||
@ -23,72 +8,33 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
|
||||
$file_tmp = $_FILES['file']['tmp_name'];
|
||||
$error = $_FILES['file']['error'];
|
||||
|
||||
// 获取当前年月,为防止报错,先设置时区
|
||||
date_default_timezone_set('Asia/Shanghai');
|
||||
$year_month = date('Y/m');
|
||||
$upload_dir = $base_upload_dir . $year_month;
|
||||
// 检查上传错误,直接提示错误代码
|
||||
if ($error !== UPLOAD_ERR_OK) {
|
||||
echo '{"error":"上传失败,错误代码:'.$error.'"}';
|
||||
exit;
|
||||
}
|
||||
|
||||
// 兼容跨平台路径
|
||||
$md5 = md5_file($file_tmp);
|
||||
$upload_dir = __DIR__ . $base_dir . substr($md5,0,1).'/'.substr($md5,1,1);
|
||||
// 检查并创建目录
|
||||
if (!is_dir($upload_dir)) {
|
||||
if (!mkdir($upload_dir, 0777, true) && !is_dir($upload_dir)) {
|
||||
echo json_encode(['error' => '无法创建目录: ' . $upload_dir]);
|
||||
echo '{"error":"无法创建目录:'.$upload_dir.'"}';
|
||||
exit;
|
||||
}
|
||||
// 开始创建index.html
|
||||
file_put_contents($upload_dir .'/index.html', 'no access');
|
||||
file_put_contents( __DIR__ . $base_dir . substr($md5,0,1). '/index.html', 'no access');
|
||||
}
|
||||
|
||||
// 生成文件扩展名
|
||||
$file_extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
||||
|
||||
// 生成随机文件名
|
||||
$randomFileName = generateRandomFileName() . '.' . $file_extension;
|
||||
|
||||
// 使用相对路径获取目标文件
|
||||
$target_file = $upload_dir . '/' . $randomFileName;
|
||||
|
||||
// 检查上传错误
|
||||
if ($error !== UPLOAD_ERR_OK) {
|
||||
switch ($error) {
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
echo json_encode(['error' => '文件过大,最大允许上传:' . $max_upload_size]);
|
||||
exit;
|
||||
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
echo json_encode(['error' => '文件部分上传失败']);
|
||||
exit;
|
||||
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
echo json_encode(['error' => '没有文件被上传']);
|
||||
exit;
|
||||
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
echo json_encode(['error' => '写入失败,无法保存文件']);
|
||||
exit;
|
||||
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
echo json_encode(['error' => '文件上传被扩展程序阻止']);
|
||||
exit;
|
||||
|
||||
default:
|
||||
echo json_encode(['error' => '上传失败,错误代码:' . $error]);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查硬盘空间
|
||||
if (disk_free_space($upload_dir) < filesize($file_tmp)) {
|
||||
echo json_encode(['error' => '硬盘空间不足,请清理磁盘后再进行操作']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 移动文件
|
||||
if (move_uploaded_file($file_tmp, $target_file)) {
|
||||
$domain = $_SERVER['HTTP_HOST'];
|
||||
$relative_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $upload_dir);
|
||||
$download_url = "http://$domain" . rtrim($relative_path, '/') . '/' . rawurlencode($randomFileName);
|
||||
echo json_encode(['upload_url' => htmlspecialchars($download_url)]);
|
||||
// 根据文件内容生成MD5文件名,这样可以排除重复上传
|
||||
$FileName = md5_file($file_tmp). '.' . strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
||||
// 这里判断下重复文件不移动文件
|
||||
if (file_exists($target_file) || move_uploaded_file($file_tmp, $upload_dir . '/' .$FileName)) {
|
||||
echo '{"upload_url":"http://'.$_SERVER['HTTP_HOST'] . $base_dir . substr($md5,0,1). '/' .substr($md5,1,1) .'/'. $FileName.'"}';
|
||||
} else {
|
||||
echo json_encode(['error' => '写入失败,无法保存文件']);
|
||||
echo '{"error":"写入失败,无法保存文件"}';
|
||||
}
|
||||
exit;
|
||||
}
|
||||
@ -188,7 +134,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
|
||||
display: flex;
|
||||
justify-content: flex-end; /* 使按钮靠右对齐 */
|
||||
margin-top: 10px; /* 添加上边距 */
|
||||
}
|
||||
}
|
||||
[url=home.php?mod=space&uid=945662]@media[/url] (max-width: 600px) {
|
||||
.container {
|
||||
width: 100%;
|
||||
@ -231,19 +177,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
|
||||
<div id="fileList" class="file-list mt-3"></div>
|
||||
<div id="statusOutput" class="status-output"></div>
|
||||
<div class="link-output">
|
||||
<button class="btn btn-info" id="directLinkBtn">直接连接</button>
|
||||
<button class="btn btn-info" id="directLinkBtn">直连代码</button>
|
||||
<button class="btn btn-info" id="htmlCodeBtn">网页代码</button>
|
||||
<button class="btn btn-info" id="forumCodeBtn">论坛代码</button>
|
||||
<button class="btn btn-info" id="markDownCodeBtn">Markdown代码</button>
|
||||
<textarea id="linkText" readonly></textarea>
|
||||
<div class="copy-all-btn-container">
|
||||
<button class="btn btn-success" id="copyAllBtn">复制所有链接</button>
|
||||
</div>
|
||||
</div>
|
||||
<h6 id="formatHint" class="card-title text-center">支持格式: JPEG, PNG, GIF, WEBP, AVIF, BMP, SVG</h6>
|
||||
</div>
|
||||
<h6 id="formatHint" class="card-title text-center">支持格式: JPEG, PNG, GIF, WEBP, AVIF, BMP, SVG 最大上传:<?php echo ini_get('upload_max_filesize');?></h6>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script>
|
||||
$(document).ready(function() {
|
||||
// 初始化时显示格式提示
|
||||
@ -279,7 +226,7 @@ $(document).ready(function () {
|
||||
var validFiles = Array.from(files).filter(file => validFormats.includes(file.type));
|
||||
|
||||
if (validFiles.length === 0) {
|
||||
showAlert('请选择有效的图片文件(JPEG, PNG, GIF,WEBP,BMP,SVG)!');
|
||||
showAlert('请选择有效的图片文件(JPEG, PNG, GIF,WEBP,AVIF,BMP,SVG)!');
|
||||
$('#files').val(''); // 清空文件选择
|
||||
return;
|
||||
}
|
||||
@ -415,17 +362,22 @@ $(document).ready(function () {
|
||||
$('#linkText').val(forumCodes);
|
||||
});
|
||||
|
||||
$('#htmlCodeBtn').on('click', function () {
|
||||
$('#markDownCodeBtn').on('click', function () {
|
||||
const forumCodes = uploadedLinks.map(url => `![${url.replace(/^.*[\\\/]/, '')}](${url})`).join('\n');
|
||||
$('#linkText').val(forumCodes);
|
||||
});
|
||||
|
||||
$('#htmlCodeBtn').on('click', function () {
|
||||
const htmlCodes = uploadedLinks.map(url => `<img src="${url}">`).join('\n');
|
||||
$('#linkText').val(htmlCodes);
|
||||
});
|
||||
});
|
||||
|
||||
$('#htmlCodeBtn').on('click', function () {
|
||||
$('#htmlCodeBtn').on('click', function () {
|
||||
const htmlCodes = uploadedLinks.map(url => `<img src="${url}">`).join('\n');
|
||||
$('#linkText').val(htmlCodes);
|
||||
});
|
||||
});
|
||||
|
||||
$('#copyAllBtn').on('click', function () {
|
||||
$('#copyAllBtn').on('click', function () {
|
||||
var copyText = $('#linkText').val();
|
||||
var tempInput = document.createElement('textarea');
|
||||
tempInput.value = copyText;
|
||||
@ -460,7 +412,7 @@ $('#copyAllBtn').on('click', function () {
|
||||
setTimeout(function () {
|
||||
document.body.removeChild(tooltip);
|
||||
}, 2200);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
$(document).on('click', '.copyButton', function () {
|
||||
|
Loading…
x
Reference in New Issue
Block a user