I use a powershell script (based on this) to monitor a folder and knowing when a file is created.
My script is the following:
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "H:\draw\"
$watcher.Filter = "*.nrrd"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action =
{
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
if ($path -like '*\tumor\*')
{
Write-Host "The file '$path' was $changeType at '$(Get-Date)'" -fore green
}
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}
When I create the file "by hand" the action is well triggered (the green sentence is displayed).
But the file is not created by hand but by a PHP script. And when the file is created by the PHP script, the action is not triggered.
But when I copy/past the file created by the PHP script, it's triggered, so it's not an issue with the file.
Is it possible to make it work with the creation under PHP?
NB: The file is created this way
$file = fopen($path, 'wb');
fwrite($file, $my_data);
fclose($file);
UPDATE
After further researches, I find out that the issue is not with PHP but when another user/session (that the one who launched the powershell script) is creating the file.
Microsoft has confirmed that this is a problem in IIS 4.0 and 5.0. This problem was first corrected in Windows 2000 Service Pack 3.
and the answer on SO is 10 years old ... I think the problem is not the mapped drive itself but when another user/session is creating the file. Because I tried on my local computer and it confirm that.