2013-09-21 27 views
-2

上傳文件後,我有一個問題,但頁面仍然顯示以前的圖像。我必須刷新頁面2-3次才能使頁面顯示正確的圖像(新圖像)。Php刪除catch不適合我

我試圖將這些行添加到頂部OG頁:

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

我也試圖改變過去的日期。但仍然無法工作。

在.htaccess我也有放:

<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> 

沒有什麼幫助,頁面仍然執行同樣的事情。每次我上傳新圖片時,我都必須刷新頁面2-3次。

我該怎麼辦?

UPDATE

一些PHP線(我有很多更但是很容易看出我剛剛發佈的重要行)

$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"); 
     } 

    } 

///在我的index.php(取後SQL)

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

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

}

回答

0

不知道它是否有幫助,但這是我們的方法來檢查瀏覽器的用戶頭像的緩存時間,如果文件被修改,我們強制瀏覽器重新驗證。否則,返回304沒有修改。

$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); 
} 

爲什麼max-age=300?這取自gravatar的緩存機制。我們相信,他們有他們的理由這樣做:)

編輯

另一種方法,虛擬的證明方式,可能使用的版本,如,用戶每次上傳圖片時,你追加與路徑?v=some_version_string強制瀏覽器重新下載該文件。

這樣做的一種方法可能是使用$the_url_of_the_image.'?v='.filemtime($the_image_path)

EDIT 2

有一件事我要的一點是你的代碼是很容易受到攻擊,但在這裏,這不是話題。不管怎樣,試試這個:

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});'> 
} 

我不知道你在哪裏存儲圖像,所以我認爲它在"myfolder/$row['user_img']"

+0

嗨,謝謝。在我把這個腳本放到我的頁面後,我收到了錯誤信息: 致命錯誤:調用未定義的函數apache_request_headers() –

+0

@PoramatFin該函數的替代方法:http://stackoverflow.com/a/9276269/534862不要在Apache中使用PHP)。此外,我已經添加了替代,更簡單的解決方案 –

+0

我已經把上面的php腳本後的功能放入頁面。我仍然收到同樣的信息。我想我很快就放棄了,似乎太複雜了。 –

0

如果你需要它FO r你自己的測試目的,你可以在谷歌瀏覽器中使用控制檯,並檢查這裏的元素:enter image description here

這將禁用緩存爲你(你不需要刷新2-3次)在任何時候,當控制檯打開。

+0

嗨,非常感謝你的回答。但是如果客戶沒有這個設置,這個問題就不能解決。它是否有其他方式來使其自動工作? –