【问题标题】:Error running process with Symfony on php server在 php 服务器上使用 Symfony 运行进程时出错
【发布时间】:2020-12-19 20:27:43
【问题描述】:

我用可洗的 php 开发了一个 API,用 opencv 在 C++ 中运行算法。应用程序将照片发送到服务器,服务器又启动一个负责处理该图像的 .exe 文件。通过终端执行文件时,算法正确处理图像。但是,为了通过 API 中的代码执行此处理,我使用的是 Symfony,更准确地说是 Process。好吧,当执行下面的行时,它总是返回给我找不到 .exe 文件。

$process = new Process(['feridas/exec', 'final.png']);

我也尝试了以下方法,但没有成功。

$process = new Process(['./exec', 'final.png']);
$process = new Process(['server-laravel/resources/feridas/exec']);

代码完成:

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Process\Process;

class ClassificationController extends Controller
{

public function process(Request $request)
{
    /** @var $image UploadedFile*/
    $image = $request->files->get('image');
    if (empty($image)) {
        throw new \Exception("Arquivo não fornecido.");
    }

    $validExtensions = ['image/jpeg', 'image/png', 'image/jpg'];

    if (!in_array($image->getMimeType(), $validExtensions)) {
        dd($image->getMimeType());
        throw new \Exception("Arquivo não suportado");

    }

    if (!$image->move(resource_path('feridas'), $image->getClientOriginalName())) {
        throw new \Exception("Error moving file.");
    }

    $newPath = resource_path('feridas/' . $image->getClientOriginalName());

    return $this->processImage($newPath);
}

private function processImage($newPath){
    $process = new Process(['ls', '-la', resource_path('feridas')]);

    $process->run();
    $process->wait();
    

    if (!$process->isSuccessful()) {
        return [
            'success' => true, //ou false
            'message'    => 'Could not execute script: %s', $process->getErrorOutput()
        ];
        throw new ProcessFailedException($process);
    }

    $output = $process->getOutput();
    return [
        'success' => true, //ou false
        'message'    => $output
    ];
 
}
}

输出:

当通过终端执行相同的算法时,它会正确返回输出,以秒为单位的算法执行时间。

【问题讨论】:

  • 您是否尝试过使用resource_path('feridas/exec') 作为路径?假设这个文件夹中有一个“exec”文件。
  • Enrico,返回错误 500
  • 评估目录,例如var_dump(__DIR__, realpath('.'), realpath('feridas/exec'))... 您使用的相对路径无法解析为您期望的路径。编辑:忘了 cwd .. var_dump(getcwd())
  • 我根据你的回答改了代码:var_dump(__DIR__, realpath('.'), realpath('feridas/exec')); $process = new Process([var_dump(getcwd())]);"sh: 1: exec: : Permission denied\n"
  • 我已经授予权限 chmod -R 777 并且在发送图像时权限已更改。

标签: php laravel serve


【解决方案1】:

如果Process类中没有cwd参数值,则getpwd() php的当前路径。

Laravel 是入口点的路径,public/index.php

var_dump(getcwd());

// /home/user/public

所以定义文件所在的绝对路径就很清楚了。

var_dump(resource_path('feridas/exec'));

// /home/user/resources/feridas/exec

试试看。

use Symfony\Component\Process\Process;

$process = new Process([resource_path('feridas/exec'), resource_path('feridas/final.png')]);

$process->run(function ($type, $buffer) {
    if (Process::ERR === $type) {
        echo 'ERR > '.$buffer;
    } else {
        echo 'OUT > '.$buffer;
    } 
});

如果这不起作用,请告诉我们下一个结果。

$process = new Process(['ls', '-la', resource_path('feridas/exec'), resource_path('feridas/final.png')]);
$process = new Process(['ls', '-la', resource_path('feridas/exec'), $newPath]);
// SSH Terminal

[root@dev ~]# /home/user/resources/feridas/exec /home/user/resources/feridas/final.png

[root@dev ~]# ls -la /home/user/resources/feridas/exec /home/user/resources/feridas/final.png

Symfony 进程文档 - https://symfony.com/doc/current/components/process.html#getting-real-time-process-output


对进一步执行结果的回答。

在第二个参数$cwd中输入要移动的目录。

//$process = new Process(['ls', '-la', resource_path('feridas')]);
$process = new Process(['./exec'], resource_path('feridas'));


// arguments
// $command = ['./exec']
// $cwd = resource_path('feridas')

检查类参数 - https://github.com/symfony/process/blob/5.x/Process.php#L141

public function __construct(array $command, string $cwd = null, array $env = null, $input = null, ?float $timeout = 60)

【讨论】:

  • Been,我按照您的建议更改了代码,在问题中进行了更新,但是算法没有与图像一起执行。退出时,它不显示算法的执行时间,就像通过终端执行它时出现的那样,在更新的问题中可以看到。
  • 我添加了...Answers to the results of further execution.
  • 非常感谢,终于可以正常使用了
猜你喜欢
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
  • 2023-03-04
  • 2014-10-31
  • 2020-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多