【发布时间】: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 并且在发送图像时权限已更改。