Image file path cannot upload to MySQL database using PHP
one text
I'm using PHP, I try to upload the image file path in MySQL Database, but the path cannot upload and it always shows as NULL my code are provide below.
if(isset($_FILES['img']) && $_FILES['img']['tmp_name'] != ''){
$fname = 'uploads/avatar-'.($id).'.png';
$dir_path =base_app. $fname;
$upload = $_FILES['img']['tmp_name'];
$type = mime_content_type($upload);
$allowed = array('image/png','image/jpeg');
if(!in_array($type,$allowed)){
$resp['msg']=" But Image failed to upload due to invalid file type.";
}else{
$new_height = 200;
$new_width = 200;
list($width, $height) = getimagesize($upload);
$t_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending( $t_image, false );
imagesavealpha( $t_image, true );
$gdImg = ($type == 'image/png')? imagecreatefrompng($upload) : imagecreatefromjpeg($upload);
imagecopyresampled($t_image, $gdImg, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if($gdImg){
if(is_file($dir_path))
unlink($dir_path);
$uploaded_img = imagepng($t_image,$dir_path);
imagedestroy($gdImg);
imagedestroy($t_image);
if(isset($uploaded_img) && $uploaded_img == true){
$qry = $this->conn->query("UPDATE users set avatar = concat('{$fname}','?v=',unix_timestamp(CURRENT_TIMESTAMP)) where id = '$id' ");
}
}else{
$resp['msg']=" But Image failed to upload due to unkown reason.";
}
}
}
I want to upload the image file path and display the image
Source