【问题标题】:Uploading a file through exec, hanging script通过exec上传文件,挂脚本
【发布时间】:2010-01-24 06:06:59
【问题描述】:

简而言之,我使用 exec() 来运行 WinSCP,这导致脚本在文件上传之前一直保持。有没有办法让脚本继续,上传在后台运行?

在 Win7 上运行 PHP 5.3.1。

【问题讨论】:

  • 换句话说:你想在 PHP 中使用多线程?现在你知道关键字了:stackoverflow.com/search?q=php+multithreading
  • 它不是经典方式的多线程,即程序有多个执行行,通常相互发送消息。他只想在后台生成另一个程序。如果您想这样称呼它,这是一种更简单的多线程方式。

标签: php upload exec


【解决方案1】:
$pipe = popen("/bin/bash ./some_shell_script.sh argument_1","r");

适用于 Linux 和 Windows。

(只是不要关闭管道,也不要尝试从中读取)

当通过 popen 生成的进程在后台运行时,您的 PHP 脚本将继续运行。当您的脚本到达文件末尾或调用 die() 时,它将等待其他进程完成并在完全退出之前关闭管道。

【讨论】:

    【解决方案2】:

    function from the PHP Manual 会有帮助吗?

    function runAsynchronously($path,$arguments) {
        $WshShell = new COM("WScript.Shell");
        $oShellLink = $WshShell->CreateShortcut("temp.lnk");
        $oShellLink->TargetPath = $path;
        $oShellLink->Arguments = $arguments;
        $oShellLink->WorkingDirectory = dirname($path);
        $oShellLink->WindowStyle = 1;
        $oShellLink->Save();
        $oExec = $WshShell->Run("temp.lnk", 7, false);
        unset($WshShell,$oShellLink,$oExec);
        unlink("temp.lnk");
    }
    

    取自PHP on a windows machine; Start process in background then Kill process

    【讨论】:

    • 这似乎不起作用。我运行了它,我也尝试了这里的所有示例somacon.com/p395.php
    【解决方案3】:

    PHP 并非设计为作为单独的线程运行,因此 apache 必须等待它完成执行。在这种情况下,您的脚本可能会在 exec 调用期间遇到时间和内存限制,这将使其成为用户 exec 的最佳解决方案。一般不推荐。

    为什么不使用 ftp 的 PHP 函数呢? http://www.php.net/manual/en/function.ftp-get.php

    【讨论】:

      猜你喜欢
      • 2012-09-18
      • 2012-05-31
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多