1

I'm building a website and I want to monitor a folder for changes, and when those changes happen I want to start my PHP script, which runs an SQL query to builds my table.
Here is the script: This is largely from nixda, so credit goes to them

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\xampp\mysql\data\db\"
    $watcher.Filter = "*myTable.csv*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                start-process -filepath \\server\path\to\php\runme.php
                Add-content "\\server\path\to\log\log.txt" -value $logline
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}
Toto
  • 89,455
  • 62
  • 89
  • 125
Dert
  • 13
  • 2
  • What have you tried? What you ask for works already by what you ask for. So WHAT is your problem? – hakre Jul 17 '21 at 15:46
  • 1
    php code is executed by php - as usual. Does this answer your question? – hakre Jul 17 '21 at 15:47
  • @hakre my problem is I want to execute the php script as soon as a .csv file is placed into the monitored folder. I'm using powershell to monitor the folder and I want to use powershell to execute runme.php after detecting the new file. What I have tried is using Invoke-Command to run the .php as it is stored on a server. – Dert Jul 18 '21 at 16:59

1 Answers1

2
  • To synchronously execute console applications or batch files and capture their output, call them directly (c:\path\to\some.exe ... or & $exePath ...), do not use Start-Process - see this answer.

  • .php script files are, at least by default, not directly executable, so you should invoke them by passing the script file path to php.exe as shown below (which runs them synchronously, with their stdout and stderr streams connected to PowerShell's streams, if needed):

    • By default, if you do invoke a .php script-file path directly or directly pass it to Start-Process's -FilePath parameter, that script is opened for editing rather than getting executed.
php.exe -f \\server\path\to\php\runme.php 
mklement0
  • 382,024
  • 64
  • 607
  • 775