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


Image file path not inserting in database

在这里,我想插入图像路径名称,并希望在文件夹中上传图像。

我正在使用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);
                       }

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

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