Update index.php

修正安装多级子目录下路径问题。
This commit is contained in:
sunson 2024-12-05 00:02:12 +08:00
parent d948a693ba
commit 35100eb2d7

View File

@ -1,23 +1,20 @@
<?php <?php
$base_dir = '/o/';//以/结束即可 $base_dir = '/o/';// 以/结束
$user = array("admin1","admin2");// 用户名,可多个 $user = array("chate.store");// 用户名,可多个
$pwd = array("chate.store1","chate.store2");// 密码,跟用户一一对应,请记得更换 $pwd = array("123456");// 密码,跟用户对应,请记得更换
if(isset($_GET['phpinfo'])){phpinfo();exit;}
$authorization = false; $authorization = false;
$key = array_search($_SERVER['PHP_AUTH_USER'], $user); $key = array_search($_SERVER['PHP_AUTH_USER'], $user);
if($key !== false){ if($key !== false && $_SERVER['PHP_AUTH_PW'] ==$pwd[$key]){$auth = true;}
if($_SERVER['PHP_AUTH_PW'] ==$pwd[$key]){
$authorization = true; if(!$auth){
} // 没登录,弹出登录窗口
}
if(!$authorization){
// 没登录成功,弹出要求输入用户名和密码窗口
header("WWW-Authenticate:Basic realm='Private'"); header("WWW-Authenticate:Basic realm='Private'");
header('HTTP/1.0 401 Unauthorized'); header('HTTP/1.0 401 Unauthorized');
exit; exit;
} }
// 处理文件上传 // 处理上传
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$name = $_FILES['file']['name']; $name = $_FILES['file']['name'];
$file_tmp = $_FILES['file']['tmp_name']; $file_tmp = $_FILES['file']['tmp_name'];
@ -31,7 +28,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
// 兼容跨平台路径 // 兼容跨平台路径
$md5 = md5_file($file_tmp); $md5 = md5_file($file_tmp);
$upload_dir = getcwd() . $base_dir . substr($md5,0,1).'/'.substr($md5,1,1); $up_dir = $base_dir . substr($md5,0,1).'/'.substr($md5,1,1);
$upload_dir = getcwd() . $up_dir;
// 检查并创建目录 // 检查并创建目录
if (!is_dir($upload_dir)) { if (!is_dir($upload_dir)) {
if (!mkdir($upload_dir, 0777, true) && !is_dir($upload_dir)) { if (!mkdir($upload_dir, 0777, true) && !is_dir($upload_dir)) {
@ -40,7 +38,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
} }
// 开始创建index.html // 开始创建index.html
file_put_contents($upload_dir .'/index.html', 'no access'); file_put_contents($upload_dir .'/index.html', 'no access');
file_put_contents( getcwd() . $base_dir . substr($md5,0,1). '/index.html', 'no access'); if(!file_exists(dirname($upload_dir) . '/index.html')){file_put_contents(dirname($upload_dir) . '/index.html', 'no access');}
if(!file_exists(dirname($upload_dir,2) . '/index.html')){file_put_contents(dirname($upload_dir,2) . '/index.html', 'no access');}
} }
// 根据文件内容生成MD5文件名这样可以排除重复上传 // 根据文件内容生成MD5文件名这样可以排除重复上传
@ -48,7 +47,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
// 这里判断下重复文件不移动文件 // 这里判断下重复文件不移动文件
$target_file=$upload_dir . '/' .$FileName; $target_file=$upload_dir . '/' .$FileName;
if (file_exists($target_file) || move_uploaded_file($file_tmp, $target_file)) { if (file_exists($target_file) || move_uploaded_file($file_tmp, $target_file)) {
echo '{"upload_url":"http://'.$_SERVER['HTTP_HOST'] . $base_dir . substr($md5,0,1). '/' .substr($md5,1,1) .'/'. $FileName.'"}'; echo '{"upload_url":"http://'.$_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']).$up_dir .'/'. $FileName.'"}';
} else { } else {
echo '{"error":"写入失败,无法保存文件"}'; echo '{"error":"写入失败,无法保存文件"}';
} }
@ -209,8 +208,9 @@ async function setBackgroundImages() {
var data = await response.json(); var data = await response.json();
var images = data.data.map(image => image.url); var images = data.data.map(image => image.url);
var backgroundDiv = document.getElementById('background'); var backgroundDiv = document.getElementById('background');
if (images.length > 0) { var baseurl = 'https://www.bing.com/';
backgroundDiv.style.backgroundImage = 'url(https://www.bing.com/' + images[0] + ')'; if (images.length > 0) {
backgroundDiv.style.backgroundImage = 'url(' + baseurl + images[0] + ')';
} }
let index = 0; let index = 0;
let currentBackgroundDiv = backgroundDiv; let currentBackgroundDiv = backgroundDiv;
@ -218,7 +218,7 @@ async function setBackgroundImages() {
const nextIndex = (index + 1) % images.length; const nextIndex = (index + 1) % images.length;
const nextBackgroundDiv = document.createElement('div'); const nextBackgroundDiv = document.createElement('div');
nextBackgroundDiv.className = 'background next'; nextBackgroundDiv.className = 'background next';
nextBackgroundDiv.style.backgroundImage = 'url(https://www.bing.com/' + images[nextIndex] + ')'; nextBackgroundDiv.style.backgroundImage = 'url(' + baseurl + images[nextIndex] + ')';
document.body.appendChild(nextBackgroundDiv); document.body.appendChild(nextBackgroundDiv);
nextBackgroundDiv.style.opacity = 0; nextBackgroundDiv.style.opacity = 0;
setTimeout(() => { setTimeout(() => {
@ -228,7 +228,7 @@ async function setBackgroundImages() {
document.body.removeChild(currentBackgroundDiv); document.body.removeChild(currentBackgroundDiv);
currentBackgroundDiv = nextBackgroundDiv; currentBackgroundDiv = nextBackgroundDiv;
index = nextIndex; index = nextIndex;
if(index>10)index=0;// 只取前十张 if(index>10)index=0;// 只取前十张
}, 1000); }, 1000);
}, 5000); }, 5000);
@ -482,4 +482,4 @@ $(document).ready(function () {
}); });
</script> </script>
</body> </body>
</html> </html>