laravel 5.2 | upload file - Call to a member function getClientOriginalName() on null

laravel 5.2 | upload file - Call to a member function getClientOriginalName() on null

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving the getClientOriginalName() on null Error in Laravel File Uploads

The error "Call to a member function getClientOriginalName() on null" is one of the most common stumbling blocks for developers handling file uploads in Laravel. It appears deceptively simple, but the root cause usually lies in how the request validation and file retrieval logic are structured. When you attempt to access properties or methods on $request->file('name') without first verifying that a file was actually uploaded or named exactly as expected, you are asking Laravel to call a method on null.

In the scenario provided, the developer attempts to retrieve the original filename immediately after defining the input field name. The code snippet shows:

$photo = $request->file('fotodosen')->getClientOriginalName();

This line assumes that $request->file('fotodosen') will always return a UploadedFile instance. However, if the form field name in the HTML does not match exactly, or if validation fails before this line executes, the helper returns null. Attempting to call getClientOriginalName() on that null value triggers the fatal error.

The Critical Role of Validation

In modern Laravel applications, you should rarely interact with $request->file() directly in your controller logic without a guardrail. The Request object is designed to be validated before business logic runs. If you bypass validation and assume the file exists, you are inviting runtime errors.

The solution is to define a validation rule set that explicitly checks for the presence of the file. Instead of manually checking with if ($request->hasFile('image')), you should use Laravel's validation facade or the validated request passed to your controller method.

Here is how the controller logic should be structured using a Form Request:

public function store(CreateDosenRequest $request)
{
    // Create the user first
    $user = User::create([
        'name' => $request->input('name'),
        'username' => $request->input('username'),
        'email' => $request->input('email'),
        'password' => bcrypt($request->input('password')), // Always hash passwords
        'admin' => $request->input('admin'),
    ]);

    // Create the Dosen record with basic data
    $dosen = Dosen::create([
        'iddosen' => $request->input('iddosen'),
        'nipy' => $request->input('nipy'),
        'namadosen' => $user->name,
        'user_id' => $user->id,
        'alamatdosen' => $request->input('alamatdosen'),
        // ... other fields
    ]);

    // Handle the file only if it passes validation and exists
    if ($request->hasFile('image')) {
        $photo = $request->file('image')->getClientOriginalName();
        
        // Use public_path() for the destination, not base_path() 
        // unless you specifically want to store outside the web root
        $destinationPath = public_path('uploads');
        
        // Ensure the directory exists before moving the file
        if (!is_dir($destinationPath)) {
            mkdir($destinationPath, 0755, true);
        }

        $request->file('image')->move($destinationPath, $photo);

        // Update the model with the path or filename as needed
        $dosen->fotodosen = $photo; 
        $dosen->save();
    }
}

Form Request Validation Rules

The CreateDosenRequest class is where you enforce the safety checks. By defining rules there, you ensure that $request->file('image') will never return null when you reach your logic block if the file upload is mandatory.

public function rules()
{
    return [
        'name' => 'required|string|max:255',
        'username' => 'required|string|max:255|unique:dosen,username',
        'email' => 'required|email|unique:dosen,email',
        'password' => 'required|min:8',
        'image' => 'nullable|file|mimes:jpg,png,jpeg|max:2048', // Optional or required depending on needs
    ];
}

Common Pitfalls to Avoid

  1. Mismatched Field Names: The form HTML defines the input as Form::file('image'), but the controller and original code referenced fotodosen. Laravel's $request->file() method is case-sensitive regarding the parameter name passed to it, though it often normalizes keys for standard inputs. Ensure the string passed to file() matches exactly what is in the <input> tag or the hidden field generated by Form::open.
  2. Using base_path() vs public_path(): In the original code, $destination was set to base_path() . '/public/uploads'. This stores files deep within the framework directory, which is generally discouraged for web-accessible assets. Use public_path('uploads') so the file resides in your project's public folder and is accessible via a URL like /uploads/filename.jpg.
  3. Double Creation: The original code attempted to create a Dosen record inside the if block after already creating one with basic data. This results in duplicate records or inconsistent state. Always update the existing model instance rather than querying for it again to create a new one.

By strictly enforcing validation rules and ensuring field name consistency, you eliminate the possibility of calling methods on a non-existent file object. This approach is not only safer but also aligns with Laravel's philosophy of handling requests through validated contracts before executing business logic.

Our network