Markup
<input type='file' name="featured_image" onchange="readURL(this);" value="<?php echo $postImage; ?>" />
Upload Function:
function upload() {
//To upload files
$errors=array();
$allowed_ext=array('jpg','jpeg','png','gif','pdf','');
$allowed_size=2097152; // 2mb max size
//Setting up $_FILE array into variables
$file_name=$_FILES['featured_image']['name'];
$file_explode=(explode('.', $file_name));
$file_ext=strtolower(end($file_explode));
$file_size=$_FILES['featured_image']['size'];
$file_tmp=$_FILES['featured_image']['tmp_name'];
//Validate if file type matched with our allowed extensions
if(!in_array($file_ext, $allowed_ext))
{
$errors[]="File Type not supported";
}
if($file_size > $allowed_size){
$errors[]="File size limit exceed";
}
if(empty($errors))
{
if(move_uploaded_file($file_tmp, 'images/featured/'.$file_name)){
$random=rand()*1200;
$file_random_name=$random.".".$file_ext;
rename("images/featured/".$file_name, "images/featured/".$file_random_name);
return $file_random_name;
} // end move uploaded files
} // end if empty errors
else
{
foreach ($errors as $key) {
echo $key;
}
}
}
Update Query:
unlink("images/featured/".$postImage);
$file_name=upload();
echo $file_name;
$Query="UPDATE blog_content SET blog_category_id='$blog_category_id',blog_content_headline='$blog_content_headline',blog_content_text='$blog_content_text',
featured_image='$file_name',content_tags='$content_tags',post_status='$post_status' WHERE blog_content_id='$blog_content_id'";
$Result=mysql_query($Query);
Now the problem is that when i run execute the update function it is updating my record without the $featured_image
. It seems that the Fileupload function is not working in the update query. Although, the unlink function is working fine. When i run the query it deletes the image from the folder but it does not upload the file.
Please note that the upload function is working totally fine when i use this in Insert Post function because in insertpost function there is no value attribute in file field. But on updatepost i have to call the existing image in the file field and I don't know why it is not uploading and saving image on update.
I've also checked $postImage
by chrome debugging and it is showing the name of my image in the value attribute.
One More thing is that if i select a new image while upating my post. Then everything works fine. But if i update post and i want the existing image remain link with my post then its not working
Please help me finding the right solution. I coudn't figure it out why the $_FILES
is not picking up the file from value.
Thanks