【问题标题】:check if file selected in field array in php检查是否在 php 的字段数组中选择了文件
【发布时间】:2016-08-09 10:04:29
【问题描述】:

我必须更新由多个文件输入类型字段组成的数据库。 所有输入字段的名称都相同。

<input type="text" name="title[]">
<input type="file" name="image[]">
<input type="text" name="title[]">
<input type="file" name="image[]">
<input type="text" name="title[]">
<input type="file" name="image[]">

现在不必选择所有图像,可能会更改所有标题但仅选择第三张图像。现在我只想在选择图像时上传文件,否则转义上传。

这是操作页面:

    <?php
      $title = $_POST['title'];

$upload = 0;
    for($i=0; $i<sizeof($title); $i++)
    {
       if(!empty($_FILES['image'][$i]))
       {
           // upload file and set flag upload=1
       } else {
           // set flag upload=0
       }

       if($upload == 1)
       {
          $qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>";
          // execute $qry
       } else {
          $qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
          // execute $qry
       }
    }

但每次只有else 语句在选择图像时运行事件,即使在未选择图像时也是如此。

更新 2:print_r($_FILES); 的结果

Array
(
    [image] => Array
        (
            [name] => Array
                (
                    [0] => 
                    [1] => Selection_238.png
                    [2] => 
                )
            [type] => Array
                (
                    [0] => 
                    [1] => image/png
                    [2] => 
                )
            [tmp_name] => Array
                (
                    [0] => 
                    [1] => /tmp/phpqSB0Jn
                    [2] => 
                )
            [error] => Array
                (
                    [0] => 4
                    [1] => 0
                    [2] => 4
                )
            [size] => Array
                (
                    [0] => 0
                    [1] => 72259
                    [2] => 0
                )
        )
)

第二个输入字段中的选定图像

【问题讨论】:

    标签: php multiple-file-upload


    【解决方案1】:

    首先,您需要将代码if($upload = 1) 中的= 更改为==。因为=assignment 运算符而不是comparisonoperator。

    检查一下,你的问题可能就解决了。否则请检查以下可能的解决方案

    1.

    <?php
      $title = $_POST['title'];
    
    for($i=0; $i<sizeof($title); $i++)
    {
       if(!empty($_FILES['image']['name'][$i]))
       {
           // check if file uploaded then run below query
            $qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>"; // you can apply if else here based on move_uploaded_file output       
       } else {
           // set flag upload=0
            $qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
       }
    }
    ?>
    

    2.

    <?php
      $title = $_POST['title'];
        $upload = 0; // define here
    for($i=0; $i<sizeof($title); $i++)
    {
       if(!empty($_FILES['image']['name'][$i]))
       {
           // upload file and set flag $upload = 1
       } else {
           // set flag $upload = 0
       }
    
       if($upload == 1) // you need to change = (assignment) to  == (comparision)
       {
          $qry = "UPDATE <table_name> SET title='$title[$i]', image='$image' WHERE <match found>";
          // execute $qry
       } else {
          $qry = "UPDATE <table_name> SET title='$title[$i]' WHERE <match found>";
          // execute $qry
       }
    }
    ?>
    

    【讨论】:

    • 我已经更正了有问题的代码(那是输入错误)。实际上,我尝试在if(!empty($_FILES['image'][$i])) { echo 'in if condition...'; } 中打印echo 'in if condition... ';,但它什么也没打印..
    • 首先尝试在任何条件之前检查echo "&lt;pre/&gt;";print_r($_FILES);。然后检查显示的内容?把它放到问题中。
    • 我用print_r($_FILES);的结果更新了问题。
    • 您还必须在此处添加['name']。请检查我编辑的答案
    猜你喜欢
    • 2019-02-25
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多