Check if file is open?

Can somebody turn me on road? I created a simple php script to rename and move files from one location to another location and zip. But i need to check if file is open and do nothing is open. If file is closed just rename and zip and move like in my script. So i need just check if file is open. Can anybody help me? Thanks!

    <?php
//error_reporting(E_ALL);
//set_time_limit(0);
$path = "MIX1/cstrike";
$path2 = "/var/www/duelnet/stable/public/";
$to_dirs = array('/demos/');
$from_dirs = array('/demos/');

$filesizes = array();
//первый проход запоминаем размеры
foreach($from_dirs as $from_dir)
    {
    $demos_dir = opendir($path.$from_dir);
    while (false!==($file=readdir($demos_dir)))
        {
        if ($file!='.'&&$file!='..'&&strpos($file,'.dem')!==false)
            {
            $fsize=filesize($path.$from_dir.$file);
            if ($fsize<50000000)
                {
                $filesizes[$file]=$fsize;
                }
            else{
    //          echo "<br/>bad file:",$file, ",  size = ", $fsize;
                }
            }
        }
    closedir($demos_dir);
    }
//echo date("h:i:s");
sleep(3);
clearstatcache ();
//второй проход пермещаем 
$i=0;
foreach($from_dirs as $from_dir)
    {
    $to_dir=$from_dirs[$i];
    $demos_dir = opendir($path.$from_dir);
    while (false!==($file=readdir($demos_dir)))
        {
        if ($file!='.'&&$file!='..'&&strpos($file,'.dem')!==false)
            {
            $fsize=0;
            $fsize=filesize($path.$from_dir.$file);
            if ($fsize<50000000)
                {
                if ($fsize==$filesizes[$file])
                    {
                    //echo "<br>ѕеремещаем файл ",$file," размер не изменилс¤; было ",$filesizes[$file]," стало, ".$fsize,";";
                    move_demo($file, $from_dir, $to_dir);
                    }
                else
                    {
                    //echo "<br>","размер изменилс¤ у файла ", $file;
                    }
                }
            else
                {
                //echo "<br/>bad file:",$file, ",  size = ", $fsize;
                }
            }
        }
    $i++;
    closedir($demos_dir);
    }

function move_demo($filename, $from_dir, $to_dir)
{
//echo $filename,"from ",$from_dir," to ",$to_dir,"<br>";
global $path, $path2;
if (file_exists($path2.$to_dir.$filename.".zip"))
    unlink($path2.$to_dir.$filename.".zip");
echo "$path$from_dir$filename\n";
echo "$path2$to_dir$filename\n\n";
$data = file_get_contents($path.$from_dir.$filename);
$gzdata = gzencode($data, 9);
unset($data);
$fp = fopen($path2.$to_dir.$filename.".zip", "xb+");
//$fp = fopen($path.$to_dir.$filename.".zip")
fwrite($fp, $gzdata);
unset($gzdata);
fclose($fp);
unlink($path.$from_dir.$filename);
}
    function move_files($src, $dst)
{
$src = '/var/www/duelnet/stable/public/demos/';
$dst = '/var/www/duelnet/stable/public/demos/';
    $dh = opendir($src);
    if (!$dh) {
        return false;
    }

    // Combine the letters before the first dash/hyphen (-),
    // and the letters after (and including) the first dot/period (.)
    // after the first dash/hyphen (-).
    $regex = '/^(.+?)-(?:.+?)(\..+?\.zip)$/';
    $moved = 0;
    $total = 0;

    while (($filename = readdir($dh)) !== false) {
        if (filetype("{$src}{$filename}") !== 'file') {
            continue;
        }

        if (!preg_match($regex, $filename)) {
            continue;
        }

        $total++;
        $new_filename = preg_replace($regex, "$1$2", $filename);
        $moved += (int)rename("{$src}{$filename}", "{$dst}{$new_filename}");
    }

    closedir($dh);

    return [$moved, $total];
}
$srcDir = '/var/www/duelnet/stable/public/demos/';
$dstDir = '/var/www/duelnet/stable/public/demos/';
$res = move_files($srcDir, $dstDir);

if (!$res) {
    // Error
}

list($moved, $total) = $res;
var_dump($moved, $total);
?>