127 lines
4.0 KiB
PHP
127 lines
4.0 KiB
PHP
<?php
|
|
@session_start();
|
|
require_once(__DIR__ . "/db_info.inc.php");
|
|
|
|
if (!(isset($_SESSION[$OJ_NAME . '_' . 'administrator'])
|
|
|| isset($_SESSION[$OJ_NAME . '_' . 'problem_editor'])
|
|
|| isset($_SESSION[$OJ_NAME . '_' . 'contest_creator']))) {
|
|
echo "<a href='../loginpage.php'>Please Login First!</a>";
|
|
exit(1);
|
|
}
|
|
|
|
$savePath = realpath(__DIR__ . '/../upload/') . '/';
|
|
$saveUrl = dirname(dirname($_SERVER['PHP_SELF'])) . '/upload/';
|
|
$extMap = array(
|
|
'image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'),
|
|
'flash' => array('swf', 'flv'),
|
|
'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb', 'mp4'),
|
|
'file' => array('pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
|
|
);
|
|
$maxSize = 400 * 1024 * 1024;
|
|
|
|
if (!empty($_FILES['imgFile']['error'])) {
|
|
switch ($_FILES['imgFile']['error']) {
|
|
case '1':
|
|
upload_error('超过php.ini允许的大小。');
|
|
break;
|
|
case '2':
|
|
upload_error('超过表单允许的大小。');
|
|
break;
|
|
case '3':
|
|
upload_error('图片只有部分被上传。');
|
|
break;
|
|
case '4':
|
|
upload_error('请选择图片。');
|
|
break;
|
|
case '6':
|
|
upload_error('找不到临时目录。');
|
|
break;
|
|
case '7':
|
|
upload_error('写文件到硬盘出错。');
|
|
break;
|
|
case '8':
|
|
upload_error('File upload stopped by extension。');
|
|
break;
|
|
case '999':
|
|
default:
|
|
upload_error('未知错误。');
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!empty($_FILES)) {
|
|
$fileName = $_FILES['imgFile']['name'];
|
|
$tmpName = $_FILES['imgFile']['tmp_name'];
|
|
$fileSize = $_FILES['imgFile']['size'];
|
|
|
|
if (!$fileName) {
|
|
upload_error('请选择文件。');
|
|
}
|
|
if (@is_dir($savePath) === false) {
|
|
upload_error('上传目录不存在。');
|
|
}
|
|
if (@is_writable($savePath) === false) {
|
|
upload_error("上传目录没有写权限。在服务器上执行下述命令解决该问题:\n chown www-data -R \"$savePath\" \n");
|
|
}
|
|
if (@is_uploaded_file($tmpName) === false) {
|
|
upload_error('上传失败。');
|
|
}
|
|
if ($fileSize > $maxSize) {
|
|
upload_error('上传文件大小超过限制。');
|
|
}
|
|
|
|
$tempArr = explode('.', $fileName);
|
|
$fileExt = strtolower(trim(array_pop($tempArr)));
|
|
$dirName = '';
|
|
|
|
foreach ($extMap as $key => $extensions) {
|
|
if (in_array($fileExt, $extensions)) {
|
|
$dirName = $key;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($dirName === '') {
|
|
$dirName = 'image';
|
|
}
|
|
if (empty($extMap[$dirName])) {
|
|
upload_error('目录名不正确。');
|
|
}
|
|
if (in_array($fileExt, $extMap[$dirName]) === false) {
|
|
upload_error('上传文件扩展名是不允许的扩展名。\n只允许' . implode(',', $extMap[$dirName]) . '格式。');
|
|
}
|
|
|
|
$savePath .= $dirName . '/';
|
|
$saveUrl .= $dirName . '/';
|
|
if (!file_exists($savePath)) {
|
|
mkdir($savePath, 0744, true);
|
|
}
|
|
|
|
$ymd = date('Ymd');
|
|
$savePath .= $ymd . '/';
|
|
$saveUrl .= $ymd . '/';
|
|
if (!file_exists($savePath)) {
|
|
mkdir($savePath, 0744, true);
|
|
}
|
|
|
|
$newFileName = date('YmdHis') . '_' . rand(10000, 99999) . '.' . $fileExt;
|
|
$filePath = $savePath . $newFileName;
|
|
if (move_uploaded_file($tmpName, $filePath) === false) {
|
|
upload_error('上传文件失败。');
|
|
}
|
|
@chmod($filePath, 0644);
|
|
|
|
upload_success($saveUrl . $newFileName);
|
|
}
|
|
|
|
function upload_success($fileUrl) {
|
|
header('Content-type: text/html; charset=UTF-8');
|
|
echo json_encode(array('error' => 0, 'url' => $fileUrl), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|
|
|
|
function upload_error($message) {
|
|
header('Content-type: text/html; charset=UTF-8');
|
|
echo json_encode(array('error' => 1, 'message' => $message), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|