2

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?

Anu
  • 857
  • 1
  • 7
  • 28
  • 2
    Browser cache. There are two solutions. 1. change url sufix (url/your-image.jpg?4626) 2. config headers: Cache-Control: no-cache – Vasyl Zhuryk Jul 04 '18 at 09:27
  • I don't understand what you meant by change url suffix. May I know what suffix you are referring to? i saw you added .jpg with some get variable as suffix. For the second method, if i do cache control, will it has any problem running other functions of the site? My website is for some file uploading, viewing, login system, file management etc – Anu Jul 04 '18 at 23:48
  • 1
    There is no way to delete browser cache of a single file.No you cannot physically delete cached file. Instead you can generate link to the file dynamically, for example file_url?some_random_str – Waqar Ul Aziz Jul 05 '18 at 00:41
  • @WaqarUlAziz I read some article about clearstatcache();. It is telling that able to clear cache using that command. I am struggling with that too. By the way I am interested to know more about generating link to the file dynamically with those random string at the back. Could you give some example how to do it? I updated my question by including the link to view the file. Could you refer to that and help? – Anu Jul 05 '18 at 00:48
  • 1
    clearstatcache() is used to clear clear cache on server not on browser, it is nothing to do in your scenario. – Waqar Ul Aziz Jul 05 '18 at 22:36
  • @WaqarUlAziz Thank you for directing me the right direction. I am able to solve the issue – Anu Jul 06 '18 at 02:06
  • @WaqarUlAziz Seems like it is possible to clear browser cache. You may read this [link](https://stackoverflow.com/questions/1037249/how-to-clear-browser-cache-with-php) – Anu Jul 10 '18 at 00:08
  • yes but it is for php or html files. For static resources e.g. image, stylesheet or javascript cache can not be cleared without adding some query string. – Waqar Ul Aziz Jul 10 '18 at 23:36
  • @WaqarUlAziz Okay. Seems like I need to learn more about cache. – Anu Jul 11 '18 at 00:48

1 Answers1

1

Based on suggestions from Stack over flow members, I am able to solve the issue by making the file opening link url dynamic and thus link always point to different file name. Here is my final working code

File unlinking and uploading block

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

File viewing block

<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'].'?'; ?><?php echo generateRandomString(); ?>" target="_blank" class="whiteLink"><span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span> View </a>
</td>

Take note about the part where after file name, I gave ? and then echo the function generateRandomString()

Function to generate Random String

Thanks to This Post for codes to generate random numbers

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
Anu
  • 857
  • 1
  • 7
  • 28