【问题标题】:PHP image upload and store the image name to MySQL table column script not working?PHP图像上传并将图像名称存储到MySQL表列脚本不起作用?
【发布时间】: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。我知道大多数人不习惯它,但我更容易使用它。

标签: php html mysql upload


【解决方案1】:

不幸的是,我还不能发布 cmets,所以我写这个作为答案。

您检查图像是否正确上传并使用它,但该函数不执行其他任何操作。然后您检查文件是否已使用is_uploaded_file 正确上传,如果不是......您继续您的例行程序。抛出异常会更明智,就像您在其他地方所做的那样。

您是否将错误报告级别设置为合理级别(以便您知道出了什么问题)?

【讨论】:

  • 该脚本存储图像名称和扩展名,因为它应该在表格的列中执行“example.jpg”,但图像没有存储在目录中。
  • 我建议您在我提到的ifs 中添加else 语句,然后再测试一次脚本。
  • 我将错误报告设置为全部。如果您有任何建议来更改脚本,让我看看它是如何工作的。
  • 你应该添加我建议的那些else 语句,看看哪里出了问题(如果有的话)。
  • 我尝试在脚本的 trigger_error() 部分中运行 else 语句,但我没有返回任何内容。我仍然不明白上传图片时失败的地方。
猜你喜欢
  • 1970-01-01
  • 2012-11-17
  • 2020-05-27
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2018-12-19
  • 2018-08-26
  • 1970-01-01
相关资源
最近更新 更多