Serving a file through a PHP script and safely replacing this file on the fly

My PHP script maintains files using readfile () and manually adds HTTP headers. Files are about ~ 1 MB in size, so partial reading is not required.

What is the best way to replace this file with an updated one, but ensure that already started downloads are not broken? I want pending downloads to be completed with the original old version of the file.

Platform: Linux, PHP 5, Apache 2.

0
source share
2 answers

Use version numbering in the file name:

$candidates=sort(glob($dir . $prefix . '*'));
$most_recent=array_pop($candidates);

eg. file0001.pdf file0002.pdf file0003.pdf

WITH.

+1
source

flock, - , , .

, LOCK_SH, fpassthru , , .

LOCK_EX, , .

+1

Source: https://habr.com/ru/post/1750477/


All Articles