Laravel Input::hasfile() 对输入数组有效吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22335937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:10:02  来源:igfitidea点击:

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

file-uploadinputlaravel

提问by MAZUMA

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.

我正在开发一个 Laravel 项目,该项目使用具有多个文件输入的表单。如果我提交表单时第一个输入为空并且所有其他输入都带有一个文件,则 hasFile 返回 false。如果第一个输入包含文件,它只会返回 true。

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".

这是通过 Input::file('file) 的输入数组。小图像输入是空的,但大图像不是。我希望它查看整个数组,如果存在任何文件,则继续“做某事”。

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?

这是预期的行为吗?或者,它应该查看整个数组吗?

回答by The Alpha

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, headreturns the first item from the array.

似乎它只检查数组中的第一个,head返回数组中的第一个项目。

回答by Ronak Shah

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

您可以使用数组键进行检查,例如如下所示:- HTML 输入类型文件元素:

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

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

拉维尔5: $request->hasFile('your_file_name.'.$key)

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

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

回答by Jesse Szypulski

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

取自 http://forumsarchive.laravel.io/viewtopic.php?id=13291

回答by DevBodin

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.

Ronak Shah 的答案真的应该在这里标记为正确的答案,当我弄清楚原因时,在 30-40 分钟试图弄清楚这个......“混乱”之后,它立即让我说“Sonnofa--”。

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

原来在输入数组上使用 hasFile() ,您需要使用点表示法。

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.

考虑到 PHP 和点表示法并没有真正结合在一起,这当然令人大开眼界。输入数组确实是问题儿童。