Reputation: 1578
Here, I am want to insert image path name and want to upload image in a folder.
I am decoding them image using base64_decode
and want to insert path of image into database. I am also inserting image into a folder.
But nothing is happening. Image is not going in folder and also not inserting image path into database.
Where I am wrong?
Here is my code:
$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);
}
Upvotes: 0
Views: 547
Reputation: 13635
This is the end result of what we discussed in the comments, and a couple of other tweaks:
$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);
}
Upvotes: 1