0

I have a converterShell class and i need to call it from controller. the command will convert a video. when i tested from the cmd, it worked, but i need to call after my file is moved to the upload folder. I thought of put the "call" on a function and call when the filme is already moved. But the problem is to call the shell class. Here are the codes:

    class ConverterShell extends AppShell {
    public function main() {
        shell_exec('cd webroot\files\ && ffmpeg -i ' .fonte. 'whatever.wmv ' .destino. 'BLABLABLA.mp4');
    }

aaaand

    public function arquivos() {
        $this->autoRender = false;

        $name = $this->request->params['form']['upload']['name'];
        $type = $this->request->params['form']['upload']['type'];
        $tmp_name = $this->request->params['form']['upload']['tmp_name'];
        $error = $this->request->params['form']['upload']['error'];
        $size = $this->request->params['form']['upload']['size'];

        $path_parts = pathinfo($name);
        $formato = ($path_parts['extension']);
        $nome = ($path_parts['filename']);          

        $tamanho = ((($size)/1024)/1024);
        $tamanho_mb = number_format($tamanho, 2, '.', '');
        $string = $name;

        echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
        if(!$error) {
            if(move_uploaded_file($tmp_name, WEBROOT_FOLDER . 'files/uploads/' . $name)) {
                $this->call function here();
                exit('OK!');
        } else {
            exit('Erro!');
        }
    }
}
4

1 回答 1

1

您应该摒弃混合控制台和应用程序逻辑的想法,它们应该分开。相反,您可以简单地将转换器功能放在一个库类中,并在您的控制器和外壳中使用它。

像这样的东西(未经测试的 CakePHP 2.x 示例代码):

/app/lib/Converter.php

class Converter {
    static public function convert($source, $destination) {
        $source = escapeshellarg(fonte . $source);
        $destination = escapeshellarg(destino . $destination);

        shell_exec('cd webroot\files\ && ffmpeg -i ' . $source . ' ' . $destination);
    }
}

/app/Console/Command/ConverterShell.php

App::uses('Converter', 'Lib');

class ConverterShell extends AppShell {
    public function main() {
        Converter::convert('whatever.wmv', 'BLABLABLA.mp4');
    }
}

/app/Controller/WhateverController.php

App::uses('Converter', 'Lib');

class WhateverController extends AppController {

    ...

    public function arquivos() {
        ...

        if(!$error) {
            if(move_uploaded_file($tmp_name, WEBROOT_FOLDER . 'files/uploads/' . $name)) {
                Converter::convert($name, $nome . '.mp4');
                exit('OK!');
            } else {
                exit('Erro!');
            }
        }
    }
}
于 2013-10-18T09:47:53.040 回答