本文将介绍如何在 PHP 中实现无刷新的多图上传功能,同时支持远程图片的下载和处理。我们将通过一个示例来展示整个过程。
1. 上传页面(upload.php)
选择图片:
2. 处理上传的 PHP 脚本(upload_handler.php)
class ImageUploader {
private $directory;
private $allowedExtensiOns= ['jpg', 'jpeg', 'png', 'gif'];
private $errors = [];
private $uploadedFiles = [];
public function __construct($directory) {
$this->directory = $directory;
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
}
public function uploadFiles($files) {
foreach ($files['file']['name'] as $key => $name) {
$tmpName = $files['file']['tmp_name'][$key];
$size = $files['file']['size'][$key];
$error = $files['file']['error'][$key];
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
if (!in_array($extension, $this->allowedExtensions)) {
$this->errors[] = "不允许的文件类型: $name";
continue;
}
if ($error !== UPLOAD_ERR_OK) {
$this->errors[] = "上传错误: $name";
continue;
}
$newName = uniqid() . '.' . $extension;
$destination = $this->directory . '/' . $newName;
if (move_uploaded_file($tmpName, $destination)) {
chmod($destination, 0777);
$this->uploadedFiles[] = $newName;
} else {
$this->errors[] = "移动文件失败: $name";
}
}
}
public function grabRemoteImage($url) {
$extension = pathinfo($url, PATHINFO_EXTENSION);
if (!in_array($extension, $this->allowedExtensions)) {
$this->errors[] = "不允许的文件类型: $url";
return;
}
$newName = uniqid() . '.' . $extension;
$destination = $this->directory . '/' . $newName;
$imageData = file_get_contents($url);
if (file_put_contents($destination, $imageData) !== false) {
chmod($destination, 0777);
$this->uploadedFiles[] = $newName;
} else {
$this->errors[] = "保存远程图片失败: $url";
}
}
public function createThumbnail($source, $destination, $width, $height) {
list($originalWidth, $originalHeight, $type) = getimagesize($source);
switch ($type) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($source);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($source);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($source);
break;
default:
$this->errors[] = "不支持的图片类型: $source";
return;
}
$thumbnail = imagecreatetruecolor($width, $height);
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
imagejpeg($thumbnail, $destination);
imagedestroy($thumbnail);
imagedestroy($image);
}
public function getErrors() {
return $this->errors;
}
public function getUploadedFiles() {
return $this->uploadedFiles;
}
}
$uploader = new ImageUploader('uploads');
if (isset($_FILES['file'])) {
$uploader->uploadFiles($_FILES);
} elseif (isset($_POST['url'])) {
$uploader->grabRemoteImage($_POST['url']);
}
foreach ($uploader->getUploadedFiles() as $file) {
$uploader->createThumbnail('uploads/' . $file, 'thumbnails/' . $file, 150, 150);
}
$respOnse= [
'error' => count($uploader->getErrors()) > 0,
'message' => implode('
', $uploader->getErrors()),
'files' => $uploader->getUploadedFiles()
];
echo json_encode($response);
?>
以上代码展示了如何实现无刷新多图上传和远程图片下载的功能。通过 `upload.php` 页面,用户可以选择多个图片进行上传,上传过程通过隐藏的 iframe 实现无刷新效果。`upload_handler.php` 脚本负责处理上传的文件,包括验证文件类型、移动文件到目标目录、创建缩略图等操作。