donglian1982 2017-09-15 19:05
浏览 26

处理输入=“文件”数组

I have a html form like so:

<form method="post" enctype="multipart/form-data">
    <input type="file" name="pictures[]" required>
    <input type="file" name="pictures[]">
    <input type="file" name="pictures[]">
    <input type="file" name="pictures[]">
    <input type="file" name="pictures[]">
</form>

such that, a user can upload up to 5 pictures. I know that I can do it with <input type="file" name="pictures[]" multiple>, but the idea is that, I want the users to be able to select files from different folders of their device(s), hence the reason why I wrote in the format above.

Now, the challenge I am having now is that, not all the inputs are required, and when the form is submitted, and I do the normal input checks $_FILES['pictures']['name], it loops through all the file array, which is normal since they have the same name.

Now my headache..

  • How do I validate only the inputs that a file is entered while maintaining the one required field?

For now, if I do a check $_FILES['pictures']['name'] when less than 5 files are added in the input boxes, it returns an error because of the input field(s) that were not entered.

EDIT

First I check if at least one file was entered, then I check if any of the files do not return any error. See my code below:

if ( count($_FILES['pictures']['name']) < 1 ) {
    echo "Please enter at least one image";
}
elseif ( $_FILES['pictures']['error'] != 0 ) {
    echo "Sorry, one or more of your uploaded images is invalid";
}

Now the form will always return an error if not all the 5 inputs fields are entered.

ANOTHER EDIT

Maybe I wasn't able to explain clearly what I wanted to achieve.. but here's the gist.

Apart from the fact that one of the inputs is required, when the form is submitted without all the fields entered, and I do something like so (skipping the required check):

$files = array();
$fileData = $_FILES['pictures'];
$files = array();
if ( is_array($fileData['name']) ) {

    for ( $i=0; $i < count($fileData['name']) ; ++$i ) {
        $files[] = array(
            'name' => $fileData['name'][$i],
            'tmp_name' => $fileData['tmp_name'][$i],
            'type' => $fileData['type'][$i],
            'error' => $fileData['error'][$i],
            'size' => $fileData['size'][$i],
        );        
    }
}
else {
    $files[] = $fileData;
}

So when I perform validations like:

foreach ($files as $file) {
    if ( $file['name'] == '' ) {
        echo "Error";
    }
}

, and the user did not enter all the images, it returns the error message.

A print_r() of the $files returns this:

Array
(
    [photos] => Array
        (
            [name] => Array
                (
                    [0] => 11356010_127900540886840_1152019271_n.jpg
                    [1] => 11370980_130679033939470_1067474802_n.jpg
                    [2] => 
                    [3] => 
                    [4] => 
                )

            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                    [2] => 
                    [3] =>
                    [4] =>  
                )

            [tmp_name] => Array
                (
                    [0] => C:\xampp7\tmp\php1C9F.tmp
                    [1] => C:\xampp7\tmp\php1CaF.tmp
                    [2] => 
                    [3] => 
                    [4] => 
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 4
                    [3] => 4
                    [4] => 4
                )

            [size] => Array
                (
                    [0] => 108492
                    [1] => 108492
                    [2] => 0
                    [3] => 0
                    [4] => 0
                )

        )

    [files] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

Is there a way I can make the empty fields to be ignore when doing my checks?

I know I can do this with javascript by checking for the empty fields and add the attribute of disabled on submit, but what if the user turned off javascript in his/her browser, what happens?

I've tried using array_filter() like so:

$filtered = array_filter($_FILES['pictures'], function($var){
    return !empty($var['pictures']['name']);
});

, but it returns an empty array. Can someone please point me to the right direction?

  • 写回答

2条回答 默认 最新

  • duan32342 2017-09-15 19:30
    关注

    $_FILES['pictures']['error'] will end up being an array of error codes, so you can loop over it and handle them explicitly. There is a good example of this in the PHP documentation.

    In your case, you may want to handle the first iteration of the loop as a special case, since you've defined the first one as being required. Something like this:

    foreach ($_FILES["pictures"]["error"] as $key => $error) {
        if ($key == 0 && $error != UPLOAD_ERR_OK) {
            // the required one didn't succeed, so we have a problem
        } else {
            // ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器