I don't know if this is a php problem or a Windows problem. I'm in Windows 10.
I have xampp installed and I'm running a custom built media library out of a web browser.
I've set it up so that when I click to play a video, php executes a batch file, created on the fly, to open up the video in VLC in fullscreen.
I also have it so, in the browser, you can click on an icon, and it will execute a batch file that opens up the video location in explorer.exe
Both of these were working fine for over two days. But today, it stopped working, notably, after we lost power (shutting off the computer) for like 5 seconds, due to a storm.
The behavior now is: the explorer window function never opens the window, and the popen
command times out with a fatal error: maximum execution time exceeded. Whereas earlier today it worked instantly.
The VLC issue is weirder. It doesn't time out. The ajax response comes back immediately, and it actually does start playing the video in VLC, HOWEVER(!!), VLC never actually opens up (now); it just plays the audio from the video in the background. I have to go to Task Manager to stop it, because VLC doesn't (visibly) launch.
Again, both of these functions were working just fine, for days since I first wrote the functions, with no issues, until the brief power-loss issue today.
So, I'm thinking it's not a php issue but maybe some Windows thing is corrupt, preventing normal behavior.
Note that if I click directly on the batch files they work properly (open up the explorer window or open up VLC in fullscreen mode). It's when I execute the batch files with popen
that the weirdness happens now.
Here's what my code looks like:
JS:
// open up VLC to play video file
$('body').on('click', 'img.hover', function()
{
var dir = $(this).data('dir');
$.post
(
ajax_url,
{
action:'play',
source:'mediacenter',
dir:dir
},
function(response)
{
console.log(response);
}
);
});
// open up folder in explorer.exe
$('body').on('click', 'div.search-field-folder-location', function()
{
var dir = $(this).data('dir');
$.post
(
ajax_url,
{
action:'explore',
source:'mediacenter',
dir:dir
},
function(response)
{
console.log(response);
}
);
});
PHP:
// open VLC
private function play()
{
$dir = $_POST['dir'];
$files = glob($dir.'/*');
$files = glob("$dir/*.{mkv,avi,mp4,webm,flv,ogv,ogg,mov,qt,wmv,m4v}", GLOB_BRACE);
if(count($files) === 1)
{
$path = "file:///C:".str_replace('\\','/', $dir);
$dir = getcwd();
$batch = 'runvlc.bat';
$handle = fopen($batch, 'w');
$data =
"CD
CD C:\Program Files (x86)\VideoLAN\VLC
start vlc -f --volume 75 \"$path\"
exit";
fwrite($handle, $data);
fclose($handle);
$cmd = '"'.$dir.'unvlc.bat"';
mc_utl::bg_exec($cmd);
return $this->response(array('status'=>'success','message'=>$files));
}
elseif(!$files)
{
return $this->response(array('status'=>'error','message'=>'No Files Found'));
}
else
{
$choices = array();
foreach($files as $file) $choices[] = basename($file);
return $this->response(array('status'=>'multiple','choices'=>$choices));
}
}
// open explorer.exe
private function explore()
{
$dir = str_replace('\\\\', '\\', 'c:\\'.str_replace('/','\\', $_POST['dir']));
$data =
'%SystemRoot%\explorer.exe "'.$dir.'"
exit';
$batch = 'opendir.bat';
$handle = fopen($batch, 'w');
fwrite($handle, $data);
fclose($handle);
mc_utl::bg_exec('"'.getcwd().'\\'.$batch.'"');
return $this->response(array('status'=>'success','data'=>$data));
}
and here's the bg_exec function:
public static function bg_exec($cmd)
{
pclose(popen($cmd, "r"));
return;
}
Same problem occurs (now) if I use shell_exec
or exec
too, whereas before the pc was shut down, all three functions (shell_exec, exec, or popen) were working.
UPDATE
I tried removing the batch file from the equation, just running the command straight into shell_exec
, like so:
chdir('C:\Program Files (x86)\VideoLAN\VLC');
shell_exec("start vlc -f \"$media\" exit");
Still doing the same thing: the VLC process is launched and the audio starts playing from the video file, but VLC's application is never visibly launched, and I have to close the process in Task Manager.
So it's not a batch file issue (running the batch file directly by dbl-clicking it works fine). It's whenever I try to launch a program from php, it only opens in the background (explorer or VLC).
I just finished running a system file check. There were no corruptions found.
This was working up until 5:00 this evening. I'm losing my mind.