php - How to avoid getting contents of the same file by separate script instances? -



php - How to avoid getting contents of the same file by separate script instances? -

let's we've got: files: a.txt, b.txt , c.txt script: script.php

we want allow script file's name (any of them), contents , delete same script runs separately can't same file.

so far:

$scan = scandir('dir'); unset($scan[0], $scan[1]); shuffle($scan); $file = $scan[0]; $contents = file_get_contents($file); if(unlink($file) !== false) homecoming $contents;

use file locking flock.

i think algorithm be:

open file get lock flock check file still exists. if has disappeared, go 7 read file delete it unlock close file, ignoring failure

step 3 required because process waiting lock 1 bound lose file, have been deleted time lock acquired.

i think deleting file when open quite safe; subsequent file operations fail. there nil wrong deleting file locked because locks not associated files, they're maintained table of file descriptors.

i think should close file @ end, though has been deleted, there operating scheme resources need cleaned up. i'm not sure if close fail or not, can ignore failure (most people ignore homecoming value close() anyway).

php

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -