将图像复制到存储文件夹 [英] Copy image to storage folder

查看:82
本文介绍了将图像复制到存储文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying copy an image to storage/app/uploads folder using the following code:

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
)

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

But when I open what has been saved, it open a text document with the original extension.

我想保存正确的方式.

推荐答案

我正在保存此代码

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);
}

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

Please try this and let me know how it works. :)

这篇关于将图像复制到存储文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆