将图像复制到存储文件夹

时间:2018-12-26 20:53:03

标签: php laravel

我正在尝试使用以下代码将图像复制到storage/app/uploads文件夹中:

public function save(Request $request)
{
        $arrayName = array();
        echo "<pre>";
        print_r($request->all());
        echo "</pre>";

        $image = $request->file('image');
        $name = $imagem->getClientOriginalName();
        $ext = $imagem->getClientOriginalExtension();
        $newName = str_replace(' ','_', $imagem->getClientOriginalName());

        $destinationPath = 'uploads';
        //$path = $imagem->store($newName);
        $path = $imagem->storeAs($destinationPath, $newName);
        echo "<pre>";
        print_r($path);
        echo "</pre>";
}

以上代码的结果是:

Array
(
    [id] => 
    [description] => description
    [title] => title
    [title_small] => title_small
    [text] => text
    [image] => Illuminate\Http\UploadedFile Object
        (
            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => manaus1.png
            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/octet-stream
            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1
            [hashName:protected] => 
            [pathName:SplFileInfo:private] => 
            [fileName:SplFileInfo:private] => 
        )

    [published] => S
)

但是当我打开保存的内容时,它会打开一个带有原始扩展名的文本文档。

我想保存正确的方式。

2 个答案:

答案 0 :(得分:1)

我正在保存此代码

if(Input::hasFile('imagen')) {
   $time = Carbon::now()->format('Y-m-d');
   $image = $request->file('imagen');
   $extension = $image->getClientOriginalExtension();
   $name = $image->getClientOriginalName();
   $fileName = $time."-".$name;
   $image->move(storage_path(),$fileName);
}

请尝试一下,让我知道它是如何工作的。 :)

答案 1 :(得分:1)

我建议您使用此:

exists