Image isn't refreshed on application - php

I'm working on a PHP application to store some personal information (including photo). I'm storing the image on a specific folder (let's say myapp/images/people/).
After saving a photo my app is redirected to a page showing the information of the specific person (kind of a profile).
If the photo is saved for the first time (no other photo was previously saved for that person) then the photo is shown in the profile. Everything seems to work at this point.
The problem is when I want to change the photo. When I replace a photo my applications keeps showing the old one. I've checked the server and the old photo is gone, there's only the new one (as I need) but the application doesn't show it.
I guess it's something to do with cache.
I've tried by adding the html tag with no cache values, I've tried by adding the same values by using the header() PHP function but nothing.
I also tried by using:
if(file_exists($imagepath))
{
unlink($imagepath);
}
and similar I've used
if(file_exists($imagepath))
{
unlink($imagepath);
clearstatcache();
}
but also nothing.
Can someone help me with this? Any idea about what's going on? The new photo is in the server, the old one is not but the app keeps showing the previous file.

The image is cached in the browser. The best way is to generate a new image name on the server and return the new image in the HTML. Since it is a new image for the browser, it won't get it from the cache and you will see the new image in the application.

Related

Image still being loaded from cache despite being updated

I am uploading a replacement image to a server using PHP file upload. It has the same name as the old image.
After the upload, the old image is still displayed from the browser cache but can, of course, be refreshed with Control+F5 on most browsers - then the new image is displayed.
I want to automatically get the new image to be displayed, without involving the users (they keep forgetting to refresh). This question has been asked and answered several times, for example at HTML - I upload new image to site, old image still loads
The solution I tried was, after the file is uploaded, then I add "fake" data to the image src attribute thus:
src="img.jpg?t=x" (where x is the time generated with php).
This works in the sense that in the file upload program I see the new image. It is said to cause the image to be loaded from the server, and not the cache. I'm not sure I understand the mechanism.
I had assumed this would also force the copy of this image to be refreshed in the cache, but the next time the image is loaded, either in the same script or a different page, in the same browser, the old image still shows. In other words the old image is still in the cache and is still being used. Of course you can refresh and then all is well.
I have tested this on several browsers including Chrome and Firefox.
Is there a way to force the new image to be loaded not just when I add the "fake" parameter, but also to be replaced in the cache. I'm sure this must be answered somewhere, but I can't find something that works.

Secure files and display them as well

My question is about HTML and PHP.
This is my setup right now:
A website where user have accounts
A FTP server with pictures (currently none)
Files are currently saved on the website in the "PICTURES" folder (which is accessible by everybody who know the full URL)
So, I would like to know how I can display the images without storing them on the website (which will fix my URL problem).
My idea was to move the files on the FTP server, and when a users logon and request a page with those images, download them through a FTP connection, save them on the website, display the images, and remove them. Which would make them accessible only between the downloading time. But this solutions sounds REALLY bad to me.
You need always to have a place where your images are stored. But, if you don't want to give a user the chance to know where are stored, you can create a system which is used to show the images.
Think about this, if you want to download a file from Mega, you can't access to the URL where the file is stored, instead of that, the server itselfs calls a system who assign you a "key" and you can download the file only through that system using your "key".
You could use a system like "base64" so you can encode your image, and show it using it, or, you can use the "header" modifier so, you can display an image using a PHP code.
For example your image tag will be like:
<img src="processImage.php?id=01&user=10&key=123" />
So, your processImage will return a "tricky" image, actually not the image, but the code processed by PHP will be returned, like using "imagejpg()" function with the header "Content-Type:image/jpeg" and then the user will not know where the image is stored actually but the img will works actually.

Found some 404s for image files in statistis

I am running a website in which I generate dynamic images for users. Initially, I was saving all the images generated forever so that I can use those images when required to be shown again but since recently I started getting some decent traffic, so I thought instead of saving and reusing the images I should generate them again when they need to be shown again.
Now what I am doing is: I have created a temp folder and I save every image in that temp folder and show it to user. And when this image is required to be shown again, I just re-create that image instead of using this temp image. And temp folder is now emptied at sometime daily.
But now I have started getting 404s in my error logs for the images. I am not getting that from where I am getting those 404s since I myself tried deleting image from tmp and then revisited that page. The image was re-created and was shown properly. I am badly stuck at this. One thing I would like you to tell is that I am allowing user to share those result-pages(with images) on social networks.
So it is like:
I create one image in tmp and show it on some result.php page. I
also put the image's link in og meta tag so that it can be properly
identified by social networks while sharing.
If somebody visits result.php page again, then it shows image again. However, if image is not available(i.e. deleted as daily
job), then it recreates it first, saves at that location and then
shows the page.
I am getting 404s for .jpg image files in error log.
On debugging it more, I found that these 404s are there because of access to images from Facebook IPs. The IPs from which 404s are detected are like: 66.220.158.119, 173.252.101.116, etc. On some googling, I founf that these IPs are allocated to Facebook.
So it means: facebook bot is accessing the images because these links were earlier shared on facebook via facebook share. Can someone please tell me is there any harm if later(say after 1 day of sharing), if facebook does not find the image anymore on my server? Will it not show anymore on shared link on facebook also?

Can I un-cache one image? Or should I do things differently?

I allow users to add a profile picture of themselves to an account, pretty standard stuff.
To make things simple, when they update their image I simply overwrite the image they currently have stored (its not a big part of what we do so simple approach)
So, page is shown with their current image, they can then choose to upload a new one, the uploaded file is then saved, image URL updated in MySQL table and page is displayed again.
The image is uploaded correctly, MySQL table updated correctly, but the image is cached so old image shows on page even though they have uploaded a new one.
Can I un cache one image? Is there a better way to do this?
For avoiding image caching you can simply add a query sting in the image src. Best would be adding a date time.
{$t = getDate();}
and in your image src add: src= "path.jpg?t=".$t
For this you need not to change anything for new image..Date time automatically gets changed every second ;)
Hope it helps..
Use versioning. In the link for the image, instead of abc.jpg say abc.jpg?v=5. And when a new image is uploaded, change the number.
see this question

PHP user profile picture cache

Okay, so on my website a user can upload a profile picture. But the issue is, if they update it by uploading and overwriting the existing profile picture they have to wait for their browser cache to clear and the same for everyone else on the site.
I know I could easily beat this by sticking a string on the end of the image URL e.g. ?id=22185 , but that will make my site loading times VERY slow.
Could any of you recommend a way of making the user's profile picture update instantly for every user on the site?
Use the file modified time as the URL variable. That way the image will be cached until that number changes, which only would happen of the file is updated.
Set unique name for each image. When user change image, e filename change too and browser will load new image instead of serving old FROM cache
You could easily add a timestamp to your files or you could use the "headers" function of PHP to change the "Expire" param.

Categories