douli8428 2013-07-31 01:06
浏览 16

文件上载在更新查询中不起作用

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大