简体   繁体   中英

Call to a member function getClientOriginalName() on a non-object with intervention Laravel

I'm having issues writing a function. When I try to update the file image, I get the error in getClientOriginalName().

Call to a member function getClientOriginalName() on array

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

Try below lines

public function uploadImg(Request $request){

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

or

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

or

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

Check this section of the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM