开发者

Background upload in PHP

开发者 https://www.devze.com 2022-12-31 07:17 出处:网络
I am working with a form that allows me to upload files via a local folder and FTP. So I want to move files over ftp (which already works)

I am working with a form that allows me to upload files via a local folder and FTP. So I want to move files over ftp (which already works)

Because of performance reasons I chose this process to run in the background so I use n开发者_JAVA百科fcftpput (linux)

In CLI the following command works perfectly: ncftpput-b-u name -p password -P 1980 127.0.0.1 /upload/ /home/Downloads/upload.zip

(Knowing that the b-parameter triggers background process) But if I run it via PHP it does not work (without the-b parameter it does)

PHP code:

$cmd = "ncftpput -b -u name -p password -P 1980 127.0.0.1 /upload/ /home/Downloads/upload.zip";
$return = exec($cmd);


Try one of the following:

1) Use the command $cmd = "ncftpput -b -u name -p password -P 1980 127.0.0.1 /upload/ /home/Downloads/upload.zip &"; (Notice the &)

2) Try php's proc_open function http://php.net/manual/en/function.proc-open.php


Try adding '&' at the end of command, this will fork it on linux level. Also try shell_exec(), if previous won't work.


Take a look at pcntl_fork. This user note has information how to correctly spawn a background process. Note that the extension that provides this function might not be activated in your PHP installation.


The best working solution for me is the following code:

function executeBackgroundProces($command) {

    $command = $command . ' > /dev/null 2>&1 & echo $!';
    exec ( $command, $op );
    $pid = ( int ) $op [0];
    if ($pid != "")
        return $pid;

    return false;
}

The command I run is: "ls bashfile" The bash file contains commands like the upload and deletion of the original files separated by ; This works fine by me

0

精彩评论

暂无评论...
验证码 换一张
取 消