PDA

View Full Version : rename file - same name - needs refresh ??


songboy
06-10-2011, 03:28 PM
Hi Folks -
I believe I have a WINcache problem.
My site allows a single photo upload. If someone wants to change their photo, I use an unlink and then a rename sequence. Say the original file is called 16.jpg, I unlink it. Now a new file upload is allowed. Lastly, the new file is renamed the same as the old file eg 16.jpg. Next, the user is able to see an example of the new photo. I have found that this works fine in Firefox, Chrome and Safari. However, in IE and Opera this sequence is only successful if, after you press submit, you press refresh. If you don't press refresh, what you see in the photo view is the old 16.jpg not the new. You only see the new photo if you press refresh.
I have looked around and from what I can gather, if, in IE, you name a file with the same name as a previous file, it will just grab the old file first because it has the name stored. I've tested this by removing the temp file 16.jpg from the temp file folder. If there is no 'old' file the photo view renders the new photo first time.
The odd thing about all this is that I swear it was working in IE sometime ago before I started testing it.
Does anyone know how to avoid/get round this 'remembering the old file' issue in IE and Opera ?
Please help as I've been staring at the screen so long I've started counting the pixels !!

domedia
06-10-2011, 05:06 PM
I have looked around and from what I can gather, if, in IE, you name a file with the same name as a previous file, it will just grab the old file first because it has the name stored. I've tested this by removing the temp file 16.jpg from the temp file folder. If there is no 'old' file the photo view renders the new photo first time.
It grabs the image from it's browser cache.

Is it any way you can append some random charcters to the filename every time it's selected? That will make sure the filename is different

jmichae3
06-13-2011, 09:50 AM
Hi Folks -
I believe I have a WINcache problem.
My site allows a single photo upload. If someone wants to change their photo, I use an unlink and then a rename sequence. Say the original file is called 16.jpg, I unlink it. Now a new file upload is allowed. Lastly, the new file is renamed the same as the old file eg 16.jpg. Next, the user is able to see an example of the new photo. I have found that this works fine in Firefox, Chrome and Safari. However, in IE and Opera this sequence is only successful if, after you press submit, you press refresh. If you don't press refresh, what you see in the photo view is the old 16.jpg not the new.

before outputting ANY HTML OR CHARACTERS, I suggest invalidating/clearing the browser cache. this should be the absolute first thing in your page. this is what I call header.php, but you can call it zap-cache.php

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: private");
header("Pragma: no-cache");
?>


this should solve your IE cache problem.

next, multiple file uploads can be handled by using array subscripts on your <input type="file"...> elements. see the php manual on file uploads, especially the .chm manual, it is especially helpful.

you get this by downloading and installing windows installer version of PHP locally from php.net or by simply downloading the documentation as extended chm file. also handy for testing some things that aren't browser-related. I add it to my PATH because I have it upload my site changes for me.

http://us.php.net/manual/en/features.file-upload.multiple.php

http://us.php.net/manual/en/function.session-cache-expire.php

songboy
06-24-2011, 10:08 PM
Hi to all.
I've tried Domedia's idea and basically it works. However, there is a problem with the idea. Simply, when you add a random aspect to the variable name, you are creating a system whereby you can't delete a file name because on each subsequent pass the old filename is rendered untouchable because the variable name is attached to a new random element. As such, there's a filename sitting in the folder that because it is random, sort of doesn't exist anymore. The result is a load of file names building up in the folder that you can't actually get rid of.

Hello Michae (I hope you see this). I've tried your idea at the top of each page that is part of the picture upload/replace scripts and nothing seems to be happening. I've used it as straight code and I've also tried it as a function but no luck so far. I appreciate that the strategy is correct but I'm not actually sure where to place it. I've put it at the very top just after the first php tag as you suggested. If you read this could you give me a specific idea about where to place the 'header' code.
Thanks to Domedia and Michae. Sadly, as of now, I'm on my third week trying to deal with this IE/Opera file cache problem. If you've read the whole thread you're probably saying why does he need to keep giving the new uploaded photo file the same file name. I only want one current photo to be linked to a particular user and to be connected via the user id. Why...well, I'm trying to avoid the build up of a folder with hundreds of old photos. It's a new one in, old one out approach. If anyone has any other ideas about how to get me out of this 'swamp' please, please let me know.
Songboy

jmichae3
06-25-2011, 02:01 AM
the header is placed at the very tippy top beginning of the PHP file, before any HTML, such as <!DOCTYPE>'s, etc.

example:

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: private");
header("Pragma: no-cache");
?><!DOCTYPE HTML>
<html>
<head>
<title>this is my title</title>
...
it would be nice if you could post the first 20 lines or so of your code, but you don't have to.

most of my php scripts don't immediately output HTML after the header, they simply process form contents to a database. THEN comes the HTML interspersed.

BTW, I got this header advice from off the net, so it's borrowed wisdom. don't remember where, but essentially it's the same thing as including the equivalent meta tags in HTML. you don't HAVE to use this PHP code, you can accomplish the same thing using meta elements:

<meta name="Expires" content="Mon, 26 Jul 1997 05:00:00 GMT">
<meta name="Last-Modified" content="Mon, 26 Jul 1997 05:00:00 GMT">
<meta name="If-Modified-Since" content="Mon, 26 Jul 1997 05:00:00 GMT">
<meta name="Cache-Control" content="private, no-cache, no-store, must-revalidate, post-check=0, pre-check=0">
<meta name="Pragma" content="no-cache">
<meta name="no-email-collection" content="link or terms">


Expires can be -1 instead of a date

for the last one, see http://www.metatags.org/meta_name_no_email_collection

domedia
06-25-2011, 06:15 PM
This is a technique I use sometimes

filename.ext?v=1

Then increase the number for every time the file changes.

jmichae3
06-26-2011, 03:26 AM
<meta name="no-email-collection" content="link or terms">
this last tag on the last line was purely optional. I threw it in because it could be useful for persuing spammers legally IF you have it on your site.

I probably should not have thrown it in there, because it did not pertain to the problem at hand. But it most certainly caught my eye when I saw it.

songboy
07-01-2011, 09:26 PM
Yo Michae -

The header thing still isn't working. Here's the top of my code:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Cache-Control: private");
header("Pragma: no-cache");
if(!isset($_SESSION))
{
session_start();
}
include("conn.php");
@$sess_id_saw_new_pic = $_SESSION['sess_id_saw_new_pic'];
$picture_look=$_SESSION['picture_look'];
@$writer_id = $_SESSION['writer_id'];


There's other bits and bobs before the html stuff (not important).
Any clues ?
Songboy

jmichae3
07-03-2011, 04:27 AM
I think this will zap your session according to the php manual (last link I gave you), see posts of php manual page). tread carefully.

since you are using sessions, according to the post, you should use

//First of all we prevent browsers from caching the image
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");


this basically eliminates the private thing.

if you are going to be tinkering with $_SESSION down below, you MUST use session_start() beforehand. this is not an if maybe condition.

songboy
07-19-2011, 05:01 PM
Hi all -
I've messed about with the header issue for another two weeks and still can't stop IE7 putting files in the temp folder. I've varied the order of headers, reduced the number of headers and placed the header code on different pages - no joy. It is the first time in three years that I've not found an answer. My only consolation is that many others seem to have similar problems stopping IE 7 caching the temp files.
I've found a way round the problem - well someone else did.
I do this:
<?php $rand = rand(100000,999999); $rand = "?" . $rand;?>
Now for image source I do this:
<img src="<?php echo $new.$rand ;?>"
$new is the actual name of the photo.
Now when you remove a current photo and replace it with another one the $rand makes it look as if it's a new photo so you don't get the same (old) photo showing before refresh is pressed.
I've read people saying that it's not a good enough solution but it works and I'll just have to accept that for the time being.
I'm still wondering about a few things. Could my issue have been caused by the fact that I'm working in WAMP and I'm working on localhost. I've researched this and it seems that this wouldn't stop the header thing working if it was going to work. If the latter rings any bells please let me know.
All the best to those that helped.
Songboy

domedia
07-19-2011, 05:37 PM
Your IE settings are probably set to 'never' check for new version of the page.
You can add a new file name, or add a variable to the file name which I posted above. They both do the same thing.

jmichae3
07-19-2011, 06:54 PM
you can actually shorten that first statement to simply <?php $rand = "?" . rand(100000,999999); ?> or for the whole thing, <img src="<?php echo $imageName . "?" . rand(100000,999999); ?>" />


I quit using WAMP due to inability to get it working or working stable or the feature set wasn't good and started using XAMPP instead, which has more features.
I usually set things up things up using virtual hosts like this http://jesusnjim.com/web-design/setup-test-server.html
but know that this ONLY works on your local machine. anything more requires a DNS server.

beware - some tech support will want to reset your hosts file, so have a backup of it elsewhere!