<?php
|
header('Content-Type: application/json; charset=utf-8');
|
|
// 设置错误报告
|
error_reporting(E_ALL);
|
ini_set('display_errors', 1);
|
|
// 允许跨域请求(如果需要)
|
header('Access-Control-Allow-Origin: *');
|
header('Access-Control-Allow-Methods: POST');
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
// 检查请求方法
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
echo json_encode([
|
'success' => false,
|
'message' => '只支持POST请求'
|
]);
|
exit;
|
}
|
|
// 获取参数
|
$uploadFileName = isset($_POST['uploadFileName']) ? $_POST['uploadFileName'] : '';
|
$file = isset($_FILES['file']) ? $_FILES['file'] : null;
|
|
// 验证参数
|
if (empty($uploadFileName)) {
|
echo json_encode([
|
'success' => false,
|
'message' => 'uploadFileName参数不能为空'
|
]);
|
exit;
|
}
|
|
if (!$file || $file['error'] !== UPLOAD_ERR_OK) {
|
$errorMessage = '文件上传失败';
|
if ($file) {
|
switch ($file['error']) {
|
case UPLOAD_ERR_INI_SIZE:
|
$errorMessage = '文件大小超过php.ini限制';
|
break;
|
case UPLOAD_ERR_FORM_SIZE:
|
$errorMessage = '文件大小超过表单限制';
|
break;
|
case UPLOAD_ERR_PARTIAL:
|
$errorMessage = '文件只有部分被上传';
|
break;
|
case UPLOAD_ERR_NO_FILE:
|
$errorMessage = '没有文件被上传';
|
break;
|
case UPLOAD_ERR_NO_TMP_DIR:
|
$errorMessage = '找不到临时文件夹';
|
break;
|
case UPLOAD_ERR_CANT_WRITE:
|
$errorMessage = '文件写入失败';
|
break;
|
}
|
}
|
echo json_encode([
|
'success' => false,
|
'message' => $errorMessage
|
]);
|
exit;
|
}
|
|
// 验证文件类型(可选)
|
$allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/gif', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
|
if (!in_array($file['type'], $allowedTypes)) {
|
echo json_encode([
|
'success' => false,
|
'message' => '不支持的文件类型:' . $file['type']
|
]);
|
exit;
|
}
|
|
// 验证文件大小(限制为10MB)
|
$maxFileSize = 10 * 1024 * 1024; // 10MB
|
if ($file['size'] > $maxFileSize) {
|
echo json_encode([
|
'success' => false,
|
'message' => '文件大小超过限制(最大10MB)'
|
]);
|
exit;
|
}
|
|
try {
|
// 创建年月目录
|
$yearMonth = date('Ym');
|
$uploadDir = "../upload/{$yearMonth}/";
|
|
// 如果目录不存在则创建
|
if (!is_dir($uploadDir)) {
|
if (!mkdir($uploadDir, 0755, true)) {
|
throw new Exception('无法创建目录:' . $uploadDir);
|
}
|
}
|
|
// 生成唯一文件名
|
$fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
|
$uniqueFileName = $uploadFileName . '_' . time() . '_' . uniqid() . '.' . $fileExtension;
|
$filePath = $uploadDir . $uniqueFileName;
|
|
// 移动上传的文件
|
if (!move_uploaded_file($file['tmp_name'], $filePath)) {
|
throw new Exception('文件保存失败');
|
}
|
|
// 如果是图片,生成缩略图
|
$thumbnailPath = '';
|
if (in_array($file['type'], ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'])) {
|
$thumbnailPath = $uploadDir . 's_' . $uniqueFileName;
|
createThumbnail($filePath, $thumbnailPath, 100, 0);
|
}
|
|
// 返回成功信息
|
$response = [
|
'success' => true,
|
'message' => '文件上传成功',
|
'data' => [
|
'originalName' => $file['name'],
|
'fileName' => $uniqueFileName,
|
'filePath' => str_replace('../', 'https://sys.966120.com.cn/', $filePath),
|
'thumbnailPath' => $thumbnailPath ? str_replace('../', 'https://sys.966120.com.cn/', $thumbnailPath) : '',
|
'fileSize' => $file['size'],
|
'fileType' => $file['type'],
|
'uploadTime' => date('Y-m-d H:i:s')
|
]
|
];
|
|
echo json_encode($response);
|
|
} catch (Exception $e) {
|
echo json_encode([
|
'success' => false,
|
'message' => '上传失败:' . $e->getMessage()
|
]);
|
}
|
|
/**
|
* 创建缩略图
|
* @param string $sourcePath 源图片路径
|
* @param string $targetPath 目标缩略图路径
|
* @param int $width 宽度
|
* @param int $height 高度(0表示按比例缩放)
|
*/
|
function createThumbnail($sourcePath, $targetPath, $width, $height) {
|
$imageInfo = getimagesize($sourcePath);
|
if (!$imageInfo) {
|
return false;
|
}
|
|
$sourceWidth = $imageInfo[0];
|
$sourceHeight = $imageInfo[1];
|
$imageType = $imageInfo[2];
|
|
// 计算缩略图尺寸
|
if ($height == 0) {
|
$height = floor($width / $sourceWidth * $sourceHeight);
|
} elseif ($width == 0) {
|
$width = floor($height / $sourceHeight * $sourceWidth);
|
}
|
|
// 创建源图像
|
switch ($imageType) {
|
case IMAGETYPE_GIF:
|
$sourceImage = imagecreatefromgif($sourcePath);
|
break;
|
case IMAGETYPE_JPEG:
|
$sourceImage = imagecreatefromjpeg($sourcePath);
|
break;
|
case IMAGETYPE_PNG:
|
$sourceImage = imagecreatefrompng($sourcePath);
|
break;
|
default:
|
return false;
|
}
|
|
if (!$sourceImage) {
|
return false;
|
}
|
|
// 创建缩略图
|
$thumbnailImage = imagecreatetruecolor($width, $height);
|
|
// 保持PNG透明度
|
if ($imageType == IMAGETYPE_PNG) {
|
imagealphablending($thumbnailImage, false);
|
imagesavealpha($thumbnailImage, true);
|
$transparent = imagecolorallocatealpha($thumbnailImage, 255, 255, 255, 127);
|
imagefilledrectangle($thumbnailImage, 0, 0, $width, $height, $transparent);
|
}
|
|
// 调整图像大小
|
imagecopyresampled($thumbnailImage, $sourceImage, 0, 0, 0, 0, $width, $height, $sourceWidth, $sourceHeight);
|
|
// 保存缩略图
|
$result = false;
|
switch ($imageType) {
|
case IMAGETYPE_GIF:
|
$result = imagegif($thumbnailImage, $targetPath);
|
break;
|
case IMAGETYPE_JPEG:
|
$result = imagejpeg($thumbnailImage, $targetPath, 90);
|
break;
|
case IMAGETYPE_PNG:
|
$result = imagepng($thumbnailImage, $targetPath, 9);
|
break;
|
}
|
|
// 释放内存
|
imagedestroy($sourceImage);
|
imagedestroy($thumbnailImage);
|
|
return $result;
|
}
|
?>
|