Laravel 5 file upload validation

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering File Uploads in Laravel: Solving the "validation.uploaded" Mystery

File uploads are one of the most common—and often trickiest—parts of building dynamic web applications. When you move from simply saving a file to ensuring that multiple users can upload and interact with those files securely, validation errors like "validation.uploaded" can appear out of nowhere. As a senior developer, understanding the lifecycle of an uploaded file within the Laravel framework is crucial for debugging these issues.

This post dives into why your friend might be encountering this error while you are not, analyzes the code you provided, and outlines the best practices for robust file handling in Laravel.

Understanding the 'validation.uploaded' Error

The error message "validation.uploaded" typically indicates that a validation rule related to a file (such as file, image, or a custom rule) failed during the request processing phase. This usually happens when you attempt to validate an uploaded file using methods like $request->hasFile() or $request->file(), but the underlying file structure, permissions, or the specific validation rules defined for that input are not met.

The key difference between your local success and your friend's failure often lies in subtle differences in how the request is being sent (e.g., headers, multipart form data encoding), environmental settings, or perhaps a difference in the file itself (size limits, type restrictions) that triggers the validation failure on the second attempt.

Reviewing Your File Upload Logic

Let's look at the code you provided to see where we can strengthen the process for both users.

The Save Video Function

Your method for saving the video seems focused only on storage:

protected function saveVideo(UploadedFile $video)
{
    $fileName = str_random(40);

    $fullFileName = $fileName.'.'.$video->guessClientExtension();

    $video->storeAs('videos', $fileName);

    return $fullFileName;
}

This function is fine for storage, assuming the $video object passed to it is a valid UploadedFile instance. However, this function doesn't handle the validation before the file even reaches this point in the controller.

The Update Controller Logic

In your controller's update method, you are attempting to validate data:

public function update(Request $request, $id)
{
    // ... other validations
    $this->validate($request, [
        'title' => 'required|unique:lessons,title,' . $video->id,
        'lesson_lists' => 'required',
        'description' => 'required'
    ]);

    // ... file handling logic
    if($request->hasFile('image')){
        $data['image'] = $this->saveImage($request->file('image'));
        // ...
    }

    if($request->hasFile('video')){
        $data['video'] = $this->saveVideo($request->file('video')); // <-- Potential point of failure
        // ...
    }
    // ...
}

The issue is likely occurring when Laravel tries to process the file input before your custom logic handles it. When using $request->hasFile() and $request->file(), you are checking for the presence of a file, but you must ensure that the request itself is correctly formatted as multipart/form-data, which can fail if the client (your friend's browser) sends the data slightly differently or if middleware is interfering.

Best Practice: Validating Files at the Request Level

To fix intermittent upload errors and ensure consistency across all users, you should leverage Laravel’s built-in validation capabilities directly on the Request object for file inputs. This forces Laravel to handle the file parsing correctly according to its rules.

Instead of relying solely on $request->hasFile(), define explicit rules in your validation array. Even if you are just checking existence, defining the rule ensures proper error reporting.

Here is how you can refine your update method:

public function update(Request $request, $id)
{
    $video = Video::findOrFail($id);

    // Define comprehensive validation rules including file checks
    $validatedData = $this->validate($request, [
        'title' => 'required|unique:lessons,title,' . $video->id,
        'lesson_lists' => 'required',
        'description' => 'required',

        // File Validation Rules: Check if the file exists and is an uploaded file
        'image' => 'nullable|image|mimes:jpeg,png,jpg|max:2048', // Example rule for image
        'video' => 'nullable|file|mimes:mp4,mov|max:10240000', // Example rule for video (10MB)
    ]);

    // Extract data safely
    $data = $request->only('title', 'description', 'lesson_lists');
    
    // We now check the validated file presence using the request object directly
    if ($request->hasFile('video')) {
        $data['video'] = $this->saveVideo($request->file('video'));
        if ($video->video !== '') {
            $this->deleteVideo($video->video); // Assuming this method exists
        }
    }

    // ... proceed with saving data and redirect
    $video->update($data);
    Session::flash("flash_notification", [
        "level" => "success",
        "message" => "Berhasil menyimpan " . $video->title
    ]);

    return redirect()->route('video.index');
}

By defining strict rules like file, mimes:mp4,mov, and max:10240000 directly in the validation step, you ensure that any request attempting to upload a file must adhere to these standards. This proactive approach eliminates ambiguity and prevents those intermittent "validation.uploaded" errors, making your application much more stable and predictable.

Conclusion

File upload handling requires meticulous attention to detail. The discrepancy between successful uploads locally and failures for other users is almost always rooted in inconsistent request data or missing validation rules. By shifting your focus from simply checking file existence (hasFile) to defining explicit, robust validation rules on the Request object itself—as demonstrated by applying rules like file and mimes—you establish a standardized contract for all file submissions. Keep building scalable applications by mastering these foundational aspects of Laravel development.