图像文件路径未插入数据库

xv8emn3q  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(286)

在这里,我想插入图像路径名,并希望上传到一个文件夹中的图像。
我正在用图像解码它们 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);
                       }
crcmnpdw

crcmnpdw1#

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

$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);
}

相关问题