doumisha5081 2013-09-21 12:45
浏览 17
已采纳

Php删除捕获对我不起作用

I have a problem after uploading file but the page still show the previous image. I have to refresh the page 2-3 times to make the page show the correct image (new image).

I tried to add these line to the top og the page:

header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 08 Nov 2014 05:00:00 GMT");

I have also tried to change the date to the past. But still not work.

In the .htaccess i have also put :

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 seconds"
  ExpiresByType image/x-icon "access plus 604800 seconds"
  ExpiresByType image/jpeg "access plus 604800 seconds"
  ExpiresByType image/png "access plus 604800 seconds"
  ExpiresByType image/gif "access plus 604800 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 604800 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 604800 seconds"
  ExpiresByType application/x-javascript "access plus 604800 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>

Nothing help, the page still perform the same thing. I have to refresh the page 2-3 times every time i uploaded new image.

What should I do ?

UPDATE

Some PHP lines (I have lots more but to be easy to see i just post the important lines)

    $photo=$_FILES['photoA']['tmp_name'];
    $photo_name=$_FILES['photoA']['name'];
    $photo_size=$_FILES['photoA']['size'];
    $photo_type=$_FILES['photoA']['type'];

    $uid=$_POST['uid'];

    $ext = strtolower(end(explode('.', $photo_name)));

    if ($ext == "jpg" or $ext == "jpeg" or $ext=="gif" or $ext=="png") {

        $filename=$uid.".".$ext;
        copy($photo,"myfolder/$filename");
        }

    }

///in my index.php (After fetch sql)

if ($row['user_img'] !="" ){

    echo"<div id='userimg_cont' style='background-image: url(../myfolder/$row[user_img]);'>

}

  • 写回答

2条回答 默认 最新

  • dongpan1871 2013-09-21 12:55
    关注

    Not sure if it helps, but this is our method to check browser's cache timing of a user's avatar and if the file was modified, we force the browser to revalidate. Else, returns 304 not modified.

    $request_headers = apache_request_headers();
    $avatar = 'path/to/user/avatar';
    $last_modified = xx; //last modified time of our avatar file
    $last_modified_str = gmdate("D, d M Y H:i:s", $last_modified).' UTC';
    
    //Not outdate
    if (isset($request_headers['If-Modified-Since']) && strtotime($request_headers['If-Modified-Since']) === $last_modified) {
        header('Last-Modified: '.$last_modified_str, true, 304);
    
    //else, outdated
    }else{
    
        $info = getimagesize($avatar);
        $mime = $info['mime'];
    
        header('Last-Modified: '.$last_modified_str, true, 200);
        header('Content-Type: '.$mime);
        header('Cache-Control: public, max-age=300');
        header('Date: '.gmdate("D, d M Y H:i:s e", time()));
        header('Expires: '.gmdate("D, d M Y H:i:s e", time()+300));
        header('Content-Disposition: inline; filename="avatar-'.$id.'.jpg"');
        readfile($avatar);
    }
    

    And why max-age=300? This is taken from gravatar's caching mechanism. We believe they have their reason to do so :)

    EDIT

    Another way, dummy proof way, is probably using version, as in, every time the user upload the image, you append the path with ?v=some_version_string to force browser to re-download the file.

    One way of doing it is probably using $the_url_of_the_image.'?v='.filemtime($the_image_path).

    EDIT 2

    One thing I have to point is your code is very prone to attack, but this is not the topic here. Anyway, try this:

    if ($row['user_img'] != ""){
        $fmtime = filemtime("myfolder/$row['user_img']");
        echo "<div id='userimg_cont' style='background-image: url(../myfolder/{$row['user_img']}?v={$fmtime});'>
    }
    

    I don't know where do you store your images so I assume it's in "myfolder/$row['user_img']".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来