MAZUMA
MAZUMA

Reputation: 953

Does Laravel Input::hasfile() work on input arrays?

I'm working on a Laravel project that uses a form with multiple file inputs. If I submit the form with the first input empty and all other inputs with a file, then hasFile returns false. It will only return true if the first input contains a file.

if(Input::hasfile('file'))
{
 // do something
}

This is the input array via Input::file('file). The small image input is empty, but the large is not. I'd like it to look at the whole array and if there any files present, then proceed with the "do something".

Array
(
    [small] => 
    [large] => Symfony\Component\HttpFoundation\File\UploadedFile Object
        (
            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image_name.jpg
            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 44333
            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
            [pathName:SplFileInfo:private] => /Applications/MAMP/tmp/php/phpHILgX2
            [fileName:SplFileInfo:private] => phpHILgX2
        )

)

Is this expected behavior? Or, should it be looking at the entire array?

Upvotes: 10

Views: 25469

Answers (5)

Stefano Amorelli
Stefano Amorelli

Reputation: 4854

At the time of writing (Laravel 8) the Request class now supports arrays for the hasFile method, as from the source code:

    /**
     * Determine if the request contains the given file.
     *
     * @param  string  $name
     * @param  string|null  $value
     * @param  string|null  $filename
     * @return bool
     */
    public function hasFile($name, $value = null, $filename = null)
    {
        if (! $this->isMultipart()) {
            return false;
        }

        return collect($this->data)->reject(function ($file) use ($name, $value, $filename) {
            return $file['name'] != $name ||
                ($value && $file['contents'] != $value) ||
                ($filename && $file['filename'] != $filename);
        })->count() > 0;
    }

Upvotes: 1

DevBodin
DevBodin

Reputation: 211

Since I can't comment, seems I'll have to post.

Ronak Shah's answer really should be marked the correct one here, and when I figured out why, it instantly had me saying "Sonnofa--" after 30-40 minutes trying to figure this... "mess" out.

Turns out to use hasFile() on an input array, you need to use dot notation.

So (using my own example) instead of

$request->hasFile("img[29][file]")

it needs to be

$request->hasFile("img.29.file")

That's certainly an eye-opener, given that PHP and dot notation don't really go together. Input arrays really are problem children.

Upvotes: 5

Ronak Shah
Ronak Shah

Reputation: 430

You can check by using the array key for example like below :- HTML Input type File Element :

<input type="file" name="your_file_name[]" />

Laravel 5 : $request->hasFile('your_file_name.'.$key)

Laravel 4.2 : Input::hasFile('your_file_name.'.$key)

Upvotes: 19

Jesse Szypulski
Jesse Szypulski

Reputation: 118

here is a snippet that may help

if(Input::hasFile('myfile')){

    $file = Input::file('myfile');

    // multiple files submitted
    if(is_array($file))
    {
        foreach($file as $part) {
            $filename = $part->getClientOriginalName();
            $part->move($destinationPath, $filename);
        }
    }
    else //single file
    {
        $filename = $file->getClientOriginalName();
        $uploadSuccess = Input::file('myfile')->move($destinationPath, $filename);
    }

} else {
    echo 'Error: no file submitted.';
}

Taken from http://forumsarchive.laravel.io/viewtopic.php?id=13291

Upvotes: 3

The Alpha
The Alpha

Reputation: 146269

Taken from source:

/**
 * Determine if the uploaded data contains a file.
 *
 * @param  string  $key
 * @return bool
 */
public function hasFile($key)
{
    if (is_array($file = $this->file($key))) $file = head($file);

    return $file instanceof \SplFileInfo;
}

It seems that it only checks the first one from the array, head returns the first item from the array.

Upvotes: 6

Related Questions