dougaimian1143 2015-08-12 03:17
浏览 196

exec / popen运行批处理文件是在后台打开程序,没有正确启动

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.

  • 写回答

1条回答 默认 最新

  • dsfdf854456 2015-08-12 04:15
    关注

    The issue turned out to have nothing to do with the unexpected reboot or anything like that.

    I had changed Apache to run as a web service, and when it is running as web service, it is not allowed to interact with desktop gui (even if you check the box allowing the apache service to interact with the desktop).

    Thus, running apache manually, rather than as a service, solves the problem. Entirely. I'll come back if I figure out how to get it to work when running as a service.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入