Laravel和Guzzle fopen():找不到文件上传到服务器

时间:2019-05-14 13:52:36

标签: php laravel stream guzzle

我正在尝试使用API​​将图像从服务器上传到另一张图像。 我已经使用本机存储Facade将其上传到服务器,然后调用Guzzle函数将文件上传到另一台服务器。 问题是我无法获取文件的本地文件。让我们看一些代码:

public function saveImage(Request $request)
    {
        // return $request;
        $consorciado = Consorciado::where('id', '=', $request->consorciado_id)->first();
        $documento = Documento::where('doc_id', '=', $request->id)->first();
        $documento = new Documento();
        $image = request()->file('f');
        $documento->consorciado_id = $consorciado->id;
        $documento->doc_id = $request->id;
        $documento->tipo = $request->tipo;
        $documento->remark = $request->remark;
        $documento->idTematica = $request->idTematica;
        $documento->field1 = $request->field1;
        $documento->field2 = $request->field2;
        $documento->field3 = $request->field3;
        $documento->field4 = $request->field4;
        $documento->field5 = $request->field5;
        $filename =  $consorciado->id . "_" . $documento->doc_id . "." . $image->getClientOriginalExtension();
        $documento->nome = $filename;
        // dd($doc);

        // $file = fopen(Storage::disk('local') . '/' .  file_get_contents($image), 'r');
        // dd($file);
        $client = new Client([
            'base_uri' => 'http://gedocflex.com.br:8003/api/file',
        ]);
        $response = $client->request('POST', 'http://gedocflex.com.br:8003/api/file', [
            'multipart' => [
                [
                    'name' => 'idTematica',
                    'contents' => '201'
                ],
                [
                    'name' => 'f',
                    // 'contents' =>  $filename, file_get_contents($image)
                    'contents' =>  fopen('/' . $filename, file_get_contents($image), 'w+')
                ],
                [
                    'name' => 'field1',
                    'contents' => '132.626.688-85'
                ],
                [
                    'name' => 'field2',
                    'contents' => '7758'
                ],
                [
                    'name' => 'field3',
                    'contents' => '404'
                ],
                [
                    'name' => 'field4',
                    'contents' => '0'
                ],
                [
                    'name' => 'field5',
                    'contents' => 'Marcello Dantas Correia'
                ],
                [
                    'name' => 'remark',
                    'contents' => 'Enviando do meu servidor para o GED'
                ],
            ]
        ]);

        return $response;


        Storage::disk('local')->put('public/' . $filename, file_get_contents($image), 'public');
        $documento->save(); //salva no meu banco
    }

我正在使用php artisan storage:link,错误是:

{message: "fopen(/1_1.jpeg): failed to open stream: No such file or directory",…}
exception: "ErrorException"
file: "/Users/marcellopato/Sites/contemplado/app/Http/Controllers/DocumentoController.php"
line: 234
message: "fopen(/1_1.jpeg): failed to open stream: No such file or directory"
trace: [{function: "handleError", class: "Illuminate\Foundation\Bootstrap\HandleExceptions", type: "->"},…]

如您所见,我得到文件的名称,但没有它的路径。 我在做什么错了?

有人吗? 预先感谢!

0 个答案:

没有答案