【问题标题】:Image file path not inserting in database图像文件路径未插入数据库
【发布时间】:2016-10-05 09:00:48
【问题描述】:

在这里,我想插入图片路径名,并想将图片上传到一个文件夹中。

我正在使用base64_decode 解码它们的图像,并希望将图像路径插入数据库。我还将图像插入到文件夹中。

但是什么都没有发生。图像没有进入文件夹,也没有将图像路径插入数据库。

我哪里错了?

这是我的代码:

$proflepic = "base64 encoded string";

$p_image = base64_decode($proflepic);
                        $im = imagecreatefromstring($p_image);

                        if ($im !== false)
                        {
                            header('Content-Type: image/jpeg');    
                            //imagejpeg($im);
                            //imagedestroy($im);

                            $target_dir = "img";

                            $filename = "image_".date('s');

                            $target_file = $target_dir.'/'.$filename;

                            if(!is_dir('../'.$target_dir))
                            {
                                 mkdir('../'.$target_dir);
                            }

                            file_put_contents($filename, $im);

                            $query  = "UPDATE ".$table." SET `profile_pic` '".$target_file."' WHERE id='".$id."'";
                            $result = $db->query($query);
                       }

【问题讨论】:

  • echo $query; chk 你查询,你在哪里定义了这个$table
  • @devpro :我确信$table$id 是正确的。
  • 不应该是file_put_contents($target_file, $im);而不是$filename吗?
  • 好吧,你在echo $query; 中得到了什么?请分享
  • SET `profile_pic` '".$target_file."' 应该是 SET `profile_pic` = '".$target_file."'。 (缺少等号)。

标签: php mysql image image-uploading


【解决方案1】:

这是我们在 cmets 中讨论的最终结果,以及其他一些调整:

$proflepic = "base64 encoded string";
$p_image   = base64_decode($proflepic);
$im        = imagecreatefromstring($p_image);

if ($im !== false)
{
    header('Content-Type: image/jpeg');    

    $target_dir = "img";
    // Changed to uniqid() instead since date('s') returns seconds,
    // which limits you to 60 images (and the risk of overwriting other images
    // are great). Also added file extension.
    $filename   = "image_" . uniqid() . '.jpg';

    $target_file = $target_dir . '/' . $filename;

    if (!is_dir('../' . $target_dir))
    {
         mkdir('../' . $target_dir);
    }

    // $im is a image resource so let's use imagejpeg() instead
    imagejpeg($im, $target_file);
    imagedestroy($im);

    // Added the missing equal sign
    $query  = "UPDATE ".$table." SET `profile_pic` = '".$target_file."' WHERE id='".$id."'";
    $result = $db->query($query);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2021-06-17
    • 2014-05-28
    相关资源
    最近更新 更多