20190912

Check if a folder is in use from PHP in a linux system

Sample code to check if a file is in use before loading data, for example if an FTP is writing to the file.

In fact we check for the entire folder, in this example a constant called DIR_XMLS, if any file is open in the operating system we abort the load.

$file=DIR_XMLS.'xxx.xml';
if (!file_exists($file)) die ("File $file does not exist, skipping step! \n");
$chkcmd="lsof|grep ".DIR_XMLS."|wc -l";
exec($chkcmd, $output, $return);
if ($output[0]>0)
{
die("Aborting process ".DIR_XMLS." is in use!\n");
}

No hay comentarios: