Laravel上传后会收到未知文件

时间:2017-12-07 15:41:47

标签: laravel laravel-5

在我的网站管理员中,我正在实现上传文件,当我设计视图并定义验证和控制器时,当我尝试使用

时,这个上传者将未知文件上传到upload目录

dd($request->all());的结果是:

array:6 [▼
  "_token" => "9tNfMnakc6viX2qYRYr6abkrJapo1qz7jG88USYf"
  "title" => "hello"
  "type" => "post"
  "images" => "Untitled-1.png"
]

验证

return [
    'title' => 'required|string|max:200|unique:contents',
    'images'=>'required|mimes:jpeg,jpg,gif,png'
];

我在控制器上的商店功能:

public function store(RequestContents $request)
{
    $featured_images = $request->file('images');
    $this->uploadImage($featured_images);
    ...
}

public function uploadImage($file)
{
    $year = Carbon::now()->year;
    $imagePath = "/uploads/post_images/{$year}/";
    $filename = $file->getClientOriginalName();
    $file->move(public_path($imagePath, $filename));
}

HTML:

<form action="manage_contents" method="POST" enctype="multipart/form-data">
...

<div class="media-body">
    <input type="file" class="file-styled" name="images">
</div>

...
</form>

输出:

enter image description here

2 个答案:

答案 0 :(得分:1)

您还需要文件名的扩展名。你使用什么laravel 5?

$filename = $file->getClientOriginalName() . $file->getClientOriginalExtension();

答案 1 :(得分:0)

尝试更改

$file->move(public_path($imagePath, $filename))

$file->move(public_path($imagePath), $filename)

作为第一个参数获取目标文件夹名称,第二个参数获取新文件名

API Reference