【发布时间】:2015-10-04 02:47:49
【问题描述】:
脚本是这样构造的:
// Function For Image Upload
public function storeUploadedImage($image) {
if ($image['error'] == UPLOAD_ERR_OK) {
// Does the Document object have an ID?
if (is_null($this->id))
trigger_error("Document::storeUploadedImage(): Attempt to upload an image for an Document object that does not have its ID property set.", E_USER_ERROR);
// Delete any previous image(s) for this Document
$this->deleteImages();
// Get and store the image filename extension
$this->imgExtension = strtolower(strrchr($image['name'], '.'));
// Store the image
$tempFilename = trim($image['tmp_name']);
if (is_uploaded_file ($tempFilename)) {
if (!(move_uploaded_file($tempFilename, $this->getImagePath())))
trigger_error("Document::storeUploadedImage(): Couldn't move uploaded file.", E_USER_ERROR);
if (!(chmod($this->getImagePath(), 0666)))
trigger_error("Document::storeUploadedImage(): Couldn't set permissions on uploaded file.", E_USER_ERROR);}
// Get the image size and type/Extension
$attrs = getimagesize ($this->getImagePath());
$imageWidth = $attrs[0];
$imageHeight = $attrs[1];
$imageType = $attrs[2];
// Load the image into memory
switch ($imageType) {
case IMAGETYPE_GIF:
$imageResource = imagecreatefromgif ($this->getImagePath());
break;
case IMAGETYPE_JPEG:
$imageResource = imagecreatefromjpeg ($this->getImagePath());
break;
case IMAGETYPE_PNG:
$imageResource = imagecreatefrompng ($this->getImagePath());
break;
default:
trigger_error ("Document::storeUploadedImage(): Unhandled or unknown image type ($imageType)", E_USER_ERROR);
}
// Copy And Resize The Image To Create The Thumbnail
$thumbHeight = intval ($imageHeight / $imageWidth * 120);
$thumbResource = imagecreatetruecolor (120, $thumbHeight);
imagecopyresampled($thumbResource, $imageResource, 0, 0, 0, 0, 120, $thumbHeight, $imageWidth, $imageHeight);
// Save the Image thumbnail
switch ($imageType) {
case IMAGETYPE_GIF:
imagegif ($thumbResource, $this->getImagePath("thumb"));
break;
case IMAGETYPE_JPEG:
imagejpeg ($thumbResource, $this->getImagePath("thumb"), 85);
break;
case IMAGETYPE_PNG:
imagepng ($thumbResource, $this->getImagePath("thumb"));
break;
default:
trigger_error ("Document::storeUploadedImage(): Unhandled or unknown image type ($imageType)", E_USER_ERROR);
}
$this->update();
}
}
// Funcion To Get The Relative Path To The Article's Fullsize Image
public function getImagePath($type="fullsize") {
return ($this->id && $this->imgExtension) ? ("/images" . "/$type/" . $this->id . $this->imgExtension) : false;
}
这是表单输入字段:
<input type="file" name="image" id="image" placeholder="Choose an image to upload" maxlength="255"/>
其余的输入存储到 MySQL 数据库表中。上传没有显示错误。图像未上传到此脚本所需的配置文件中定义的指定目录
【问题讨论】:
-
您的表单是否有 attr enctype="multipart/form-data"?
-
为什么要重新发明轮子? FWIW,我为此使用了一个开源图库应用程序。我觉得我用的那个已经不支持了,但是我敢肯定肯定有几十个替代品。
-
请正确缩进您的代码
-
@Thi Tran 我确实有这样的 attr 设置。
-
@Cliff Burton 对此我很抱歉。从一开始我就一直在使用这样的 PHP。我知道大多数人不习惯它,但我更容易使用它。