【问题标题】:PHP not detecting file input valuePHP没有检测到文件输入值
【发布时间】:2015-05-24 01:25:09
【问题描述】:

我正在尝试创建一个用户可以插入多个图像的表单。但是,当文件输入为空时,类函数 (addImgToNieuws) 仍会运行。

代码如下:

if($_POST && !empty($_POST['title']) && !empty($_POST['description']) ) {
    $response = $mysql->addNieuwsItem(
        $_POST['title'], 
        $_POST['description'],
        $id
    );

    if(!empty($_FILES['images']) && $_FILES['images']['error'] != 4){
        $response = $mysql->addImgToNieuws(
            $_FILES['images']
        );
    }
}

形式:

<form action='' method='post' enctype='multipart/form-data' />
    <input type='text' name='title' placeholder='Titel' />
    <textarea name='description' placeholder='Descriptie'></textarea>
    <input type='file' name='images[]' multiple />
    <input type='submit' name='submit' value='Plaatsen' />
</form>

类函数:

function addImgToNieuws($images){
    echo 'Function runs';
}

编辑:这是否与它作为数组发布的事实有关?

【问题讨论】:

  • 你在这里使用什么 ORM,或者你有没有自己制作? addNieuwsItem 不是标准的 PHP 函数。
  • 你为什么要检查4的错误代码?仅当值为 0 时,您才应继续上传。
  • 试试if(!empty($_FILES['images']) &amp;&amp; $_FILES['images']['error'] === UPLOAD_ERR_OK)
  • 感谢@jeroen - 我只是使用了正确的 PHP 常量而不是 (int) 0 ;)
  • 大声笑-我们错过了我认为的明显...$_FILES['images'] 将是一个数组,因为您要进行多次上传...您将需要循环它@987654330 @ 然后检查每个$iError === UPLOAD_ERR_OK ;)

标签: php mysql forms input mysqli


【解决方案1】:

由于您要上传多个文件,$_FILES['images'] 将是一个数组,您需要相应地处理每个图像上传和错误陷阱。

但是,您的 addImgToNieuws() 方法看起来好像一次性处理了整个 $_FILES['images'] 数组,因此与其多次调用它,不如只记录(或捕获/输出)任何失败。

if(!empty($_FILES['images'])) {

    $aErrors = array();
    foreach($_FILES['images'] as $aThisImage) {

        // capture any errors
        // I've put the current $_FILES['images'] array into the errors
        // array so you can check the ['name'], ['tmp_name'] or ['error']
        // for each individually
        if($aThisImage['error'] !== UPLOAD_ERR_OK) { 
            $aErrors[] = $aThisImage;
        }
    }

    //check the errors
    if($aErrors) {
        // take appropriate action for your app knowing that
        // there has been a problem with *some* images
    }

    //no errors
    else {
        $response = $mysql->addImgToNieuws(
            $_FILES['images']
        );
    }
}

【讨论】:

  • 完美,但我认为 foreach 循环应该是这样的:foreach($_FILES['images']['error'] as $aThisImage) { if($aThisImage !== UPLOAD_ERR_OK) { $aErrors[] = $aThisImage; } }
  • 这只会为您提供在 $aErrors 数组中失败的每个图片上传的错误代码 - 您无法确定 哪个 图片上传实际上失败了 -但是,如果您对此感到满意,那就可以了。
【解决方案2】:

试试这个

 if(!empty($_FILES['images']) && $_FILES['images']['error'] != 4 && $_FILES['images'] != ''){

【讨论】:

【解决方案3】:

你可以这样试试:

if(isset($_POST['submit']) && isset($_POST['title']) && isset($_POST['description']) ) {
    $response = $mysql->addNieuwsItem(
        $_POST['title'], 
        $_POST['description'],
        $id
    );

    if(is_uploaded_file($_FILES['images']['tmp_name'])){
        $response = $mysql->addImgToNieuws(
            $_FILES['images']
        );
    }
}

【讨论】:

  • 警告:is_uploaded_file() 期望参数 1 是字符串,数组在第 17 行的 /Applications/MAMP/htdocs/hb/nieuws.php 中给出,所以我想我们找到了问题......一个数组..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 2019-02-12
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多