Scenario
I have a file uploader where I can upload files. There is another option to update the previously uploaded file. Also I can view the uploaded file using some link.
Requirement
Everything was working fine. Lets say I uploaded fileA. I can view this file using a link. Then I updated that file with another file named fileB. I can view this file using the same link. The issue here is, if I used same file name to upload second time, it will have error. Thus I modified my code the following way.
if(file_exists($target_fileCadEdit)){
unlink($target_fileCadEdit);
move_uploaded_file($_FILES["fileCad"]["tmp_name"], $target_fileCadEdit);
}else{
move_uploaded_file($_FILES["fileCad"]["tmp_name"], $target_fileCadEdit);
}
Problem
The old file is deleted and updating with the new file (Take note same file name for old and new file) correctly in the same location. But when I use the same link to view the updated file, it is still showing old file. I physically checked the uploaded file and the old file was not there anymore and updated with the new file. But still when I click view using the link, it is showing old file. Not sure from where program is accessing that deleted file.
Can anyone help?
Edit
Found out issue is due to browser cache. I tried to manually remove browser cache and tried again to view file and now it is showing the actual file present in the server.
Problem Now
Now the problem is, how to clear cache automatically while running script. Some suggestion is to try around with header tags. But I am afraid, it may affect performance if I configured wrongly. So I used another method clearstatcache();
. But it seems not working too. Please see my code now
if(file_exists($target_fileCadEdit)){
unlink($target_fileCadEdit);
clearstatcache();
move_uploaded_file($_FILES["fileCad"]["tmp_name"], $target_fileCadEdit);
clearstatcache();
}else{
move_uploaded_file($_FILES["fileCad"]["tmp_name"], $target_fileCadEdit);
}
Here is my link to view the file. It is using many dynamic variables
<td class="viewEditTd">
<a href="<?php echo $locationForViewLink; ?><?php echo $slash; ?><?php echo $_POST['searchInput']; ?><?php echo $cadguiFolder; ?><?php echo $row['cadRevision']; ?><?php echo $slash; ?><?php echo $row['cadFile']; ?>" target="_blank" class="whiteLink"><span class="
glyphicon glyphicon-folder-open" aria-hidden="true"></span> View </a>
</td>
Any clue anyone?