通过干预Laravel调用非对象上的成员函数getClientOriginalName()

时间:2018-11-27 16:28:04

标签: php laravel laravel-filesystem

我在编写函数时遇到问题。当我尝试更新文件图像时,出现getClientOriginalName().

中的错误
  

在数组上调用成员函数getClientOriginalName()

<?php

/**
 * Upload File.
 *
 * @param array $input
 * @return array $input
 */
public function uploadImg($input)
{
    if (isset($input['featured_image']) && !empty($input['featured_image'])) {
        $avatar = $input['featured_image'];
        $fileName = time() . $avatar->getClientOriginalName();
        $this->storage->put($this->upload_path . $fileName, file_get_contents($avatar->getRealPath()));
        $path = 'posts_images/' . $fileName;
        $input = array_merge($input, ['featured_image' => $path]);
    } elseif (isset($input['img']) && !empty($input['img'])) {
        $avatar = $input['img'];
        $fileName = time() . $avatar->getClientOriginalName();
        $this->storage->put($this->upload_path . $fileName, file_get_contents($avatar->getRealPath()));
        $path = 'posts_images/' . $fileName;
        $input = array_merge($input, ['img' => $path]);
    }

    return $input;
}

1 个答案:

答案 0 :(得分:2)

尝试以下行

public function uploadImg(Request $request){

  $request->file('featured_image')->getClientOriginalName();
}

$file = Input::file('featured_image');
$file->getClientOriginalExtension(); 
$file->getClientOriginalName();

Input::file('featured_image')->getClientOriginalExtension();

查看文档的this section