php 服务器上的 Symfony 进程错误 运行
Error running process with Symfony on php server
我在 php 中开发了一个 API with washable to 运行 一个用 C++ 和 opencv 的算法。应用程序向服务器发送一张照片,服务器启动一个 .exe 文件负责处理该图像。当通过终端执行文件时,算法正确处理了图像。但是,要通过 API 中的代码执行此处理,我使用的是 Symfony,更准确地说是 Process。好吧,当执行下面的行时,我总是 returns 找不到 .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
];
}
}
输出:
当通过终端执行相同的算法时,它正确地 returns 输出,算法的执行时间(以秒为单位)。
如果进程class中没有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')
检查 class 个参数 - 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)
我在 php 中开发了一个 API with washable to 运行 一个用 C++ 和 opencv 的算法。应用程序向服务器发送一张照片,服务器启动一个 .exe 文件负责处理该图像。当通过终端执行文件时,算法正确处理了图像。但是,要通过 API 中的代码执行此处理,我使用的是 Symfony,更准确地说是 Process。好吧,当执行下面的行时,我总是 returns 找不到 .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
];
}
}
输出:
当通过终端执行相同的算法时,它正确地 returns 输出,算法的执行时间(以秒为单位)。
如果进程class中没有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')
检查 class 个参数 - 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)