取消链接上一个文件后更新PHP文件路径

时间:2013-11-07 05:44:34

标签: php file-upload

在我的php文件中使用文件上传控件。取消链接以前的文件并使用新文件更新后,它不会使用新的图像文件进行更新。当我刷新它显示新图像时,它显示了列表中的上一个图像。在单个php文件中使用列表和更新屏幕。我用文件缓存清除了。即使它不起作用。

 <?php 
     require_once("../config/config.php");
     header("Cache-Control: no-cache, must-revalidate");
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    <?php
     require_once("../images.inc.php"); 
     if(Tools::isLoggedIn('admin') == false)
     { 
        Tools::redirectTo($link->rewrite_link(array("template"=>ADMIN_URI."index")));   
     } 
     $id_product =  (Tools::getVar("id_product")) ? Tools::getVar("id_rpoduct") : 0; 
     $page = intval(Tools::getVar("page"));
     $product = new Product($id_product);
     if(Tools::isSubmit("OnSave"))
     {
        $id_product =  Tools::getVar("id_product");
        $product = new Product($id_product);
        $product->name = Tools::getVar("name");
        $product->id_category = Tools::getVar("id_category");
        $product->short_description = Tools::getVar("short_description");
        $product->code = Tools::getVar("code");
        $product->weight = Tools::getVar("weight");
        $product->price = Tools::getVar("price");
        $product->size = Tools::getVar("size");
        $product->meta_title = Tools::getVar("meta_title"); 
        $product->meta_description = Tools::getVar("meta_description"); 
        $product->meta_keyword = Tools::getVar("meta_keyword"); 
        $product->status = Tools::getVar("status");     
        if(($product->name)=="")
        {
            $errors['err_name'] = "Name is required";
        }
        if(($product->short_description)=="")
        {
            $errors['err_short_description'] = "Description is required";
        }//edit on
        if($product->id_category <= 0)
        {
            $errors['err_id_category'] ="Parent Category is required";

        }
        if($product->code == '')
        {
            $errors['err_code'] ="Product Code is required";

        }
    if(sizeof($errors) == 0) 
         {
                if($id_product) {
                $product->date_upd = date('Y-m-d H:i:s');
                if(isset($_FILES) AND sizeof($_FILES) AND $_FILES['image']['name'] != NULL && $id_product) {
                    $postimage = $product->postImage($id_product);              
                }           
                if($product->update())
                {
                    $redirect =  array("template"=>ADMIN_URI."product");
                    $session->set("success","Product Updated Successfully");
                    Tools::redirectTo($link->rewrite_link($redirect));  
                }
                else {
                    $errors['fail'] ="Please Try again Later";
                }
            }
            else {
                $product->date_add = date('Y-m-d H:i:s');
                $product->date_upd = date('Y-m-d H:i:s');
                $id_product = $product->add(DB::RETURN_INSERT_ID);          
                if(isset($_FILES) AND sizeof($_FILES) AND $_FILES['image']['name'] != NULL && $id_product) {
                    $product->postImage($id_product);
                }
                if($id_product){
                    $redirect = array("template"=>ADMIN_URI."product");
                    $session->set("success","Product Added Successfully");
                    Tools::redirectTo($link->rewrite_link($redirect));
                }
                else {
                    $errors['fail'] ="Please Try again Later";
                }
            }
         }
    }
    if(Tools::getVar('action') == 'delete' && Tools::getVar('id_product') ) 
    {
        $id_product = Tools::getVar('id_product'); 
        $product = new Product($id_product);
        if($product->delete()) {
            $session->set("success","Product Deleted successfully");
            Tools::redirectTo($link->rewrite_link(array("template"=>ADMIN_URI."product"))); 
        }
        else {
            $errors['fail'] ="Please Try again Later";
        }

     }
     if(Tools::getVar('action') == "status_change"  && Tools::getVar('id_product'))
     {
        $id_product = Tools::getVar('id_product'); 
        $product = new Product($id_product);
        $product->status =Tools::getVar("status");
        $product->update();
        $session->set("success","Status updated successfully");
        Tools::redirectTo($link->rewrite_link(array("template"=>ADMIN_URI."product"))); 
     }
     require_once("header.php");
    ?>

我的图片上传课程,

 <?php
    class Product extends ObjectModel
    {
    protected function uploadImage($id, $name, $dir, $ext = false)
        {
            if (isset($_FILES[$name]['tmp_name']) AND !empty($_FILES[$name]['tmp_name']))
            {           
                // Delete old image
                if (file_exists($dir.$id.'.'.$this->imageType)) {
                    $imagesTypes = $this->productImageTypes;
                    foreach ($imagesTypes AS $k => $imageType)
                    {
                        if (file_exists($dir.$id.'-'.stripslashes($imageType['name']).'.jpg')) 
                        {
                            unlink($dir.$id.'-'.stripslashes($imageType['name']).'.jpg');                       
                        }
                    }
                    unlink($dir.$id.'.'.$this->imageType);
                }
                // Check image validity
                if ($error = checkImage($_FILES[$name], $this->maxImageSize))
                    $_errors[] = $error;
                elseif (!$tmpName = tempnam(TEMP_DIR, 'SST') OR !move_uploaded_file($_FILES[$name]['tmp_name'], $tmpName))
                    return false;
                else
                {
                    $_FILES[$name]['tmp_name'] = $tmpName;              

                    // Copy new image
                    if (!imageResize($tmpName, $dir.$id.'.'.$this->imageType, NULL, NULL, ($ext ? $ext : $this->imageType)))
                        $_errors[] = Tools::displayError('An error occurred while uploading image.');
                    else {
                        $imagesTypes = $this->productImageTypes;
                        foreach ($imagesTypes AS $k => $imageType)
                            imageResize($dir.$id.'.jpg', $dir.$id.'-'.stripslashes($imageType['name']).'.jpg', (int)($imageType['width']), (int)($imageType['height']));
                    }
                    if (sizeof($_errors))
                        return false;
                    else
                    {
                        unlink($tmpName);
                        return true;
                    }
                    return false;
                }
            }
            return true;
        }
    }

1 个答案:

答案 0 :(得分:2)

假设您的浏览器正在显示图像的缓存版本,因为您将新数据保存为相同的文件名,请尝试在生成HTML以显示它时将包含随机字符串或时间戳的查询字符串附加到图像URL。像这样:

PHP

$image_tag='<img src="image-file.jpg?t='.timestamp().'" />';

这应该使浏览器获取图像的新版本,而不是使用它的缓存版本。