PHP图像上传并将图像名称存储到MySQL表列脚本不工作?
脚本的构造如下所示:PHP图像上传并将图像名称存储到MySQL表列脚本不工作?,php,html,mysql,upload,Php,Html,Mysql,Upload,脚本的构造如下所示: // 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::s
// 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,我使用一个开源的画廊应用程序。我想我使用的那个已经不受支持了,但我确信肯定会有几十种选择。请正确缩进你的code@ThiTran我确实有这样的attr设置。@Cliff Burton对此我很抱歉。我从一开始就这样使用PHP。我知道大多数人不习惯使用它,但对我来说,使用它更容易。脚本将图像名称和扩展名存储在表的列中,正如它应该做的那样example.jpg,但图像没有存储在目录中。我建议您将else语句添加到我提到的ifs中,然后再次测试脚本。我将错误报告设置为“全部”。如果您有任何建议来更改脚本,让我看看它是如何工作的。您应该添加我建议的其他语句,看看是否有任何错误。我尝试在脚本的触发器错误部分运行其他语句,但没有得到任何结果。我仍然不明白这在上传图像时失败的地方。