在多个文件上传时收到致命错误

时间:2015-10-06 14:31:12

标签: php mysql image file upload

我制作了一个脚本,可以使用表单上传多个文件:

ConcurrentModificationException

javascript代码应该在用户选择文件时提交表单,这里是我用来处理上传的php:

public void fireListener() {
  for(Listener l : listeners.toArray(new Listener[listeners.size()])) {
    if(!listeners.contains(l)) continue;
    l.someMethod();
  }
}

这给了我一个错误:

  

致命错误:第24行的C:\ xampp \ htdocs \ tp \ upload_image.php

第24行是包含以下内容的行:<form action="upload_image.php" id="form_img" method="POST" enctype="multipart/form-data"> <div align="center"> <div class="fileUpload btn btn-primary"> <span>Carica immagini nella galleria</span> <input type="file" name="immagini[]" multiple="multiple" id="file_img" class="upload"/> <script> document.getElementById("file_img").onchange = function() { document.getElementById("form_img").submit(); }; </script> </div> </div> </form>

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用插入->execute(array())的数组尝试绑定。如果您想确保它们应该是值,那么只需在foreach()循环中进行一些验证。最后一件事,你说你的表单会进行多次上传,但只有一个输入,只要输入发生变化就会上传,所以这有点令人困惑:

// I am just saving your connection to a function just to clean it up a bit
function connection()
    {
        include(__DIR__."/config/db.php");
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $dbh;
    }
// I like to reogranize my $_FILES array so each file is in it's own array
function organize($array = false)
    {
        if(!is_array($array) || empty($array))
            return $array;

        foreach($array['name'] as $key => $value) {

                if($array['error'][$key] != 0) {
                        $files[$key]    =   false;
                        continue;
                    }

                $files[$key]    =   array(
                                            "name"      =>  $array['name'][$key],
                                            "tmp_name"  =>  $array['tmp_name'][$key],
                                            "type"      =>  $array['type'][$key],
                                            "error"     =>  $array['error'][$key],
                                            "size"      =>  $array['size'][$key]
                                        );
            }

        return $files;
    }

// This will return an array of bind values and statement values
function CompileUpload($use_name = 'immagini')
    {
        // If empty, do nothing
        if(empty($_FILES[$use_name]))
            return false;

        //Reorganize array
        $FILES  =   organize($_FILES[$use_name]);
        $return =   false;
        foreach ($FILES as $i => $file) { 
            if($file["error"] !== 0)
                continue;
            // I would suggest just saving the name and location of
            // the file(s) instead of saving them to the database. 
            $temp = $file["tmp_name"];
            $name = $file["name"];
            $type = $file["type"];
            $data = file_get_contents($temp);
            // Create a bind array
            $bind[":".$i."name"]    =   $name;
            $bind[":".$i."type"]    =   $type;
            $bind[":".$i."data"]    =   $data;
            // Create the append values for the sql statement
            $bCols[$i][]    =   ":".$i."name";
            $bCols[$i][]    =   ":".$i."type";
            $bCols[$i][]    =   ":".$i."data";
            // Implode and save to a master row array
            $iCols[]        =   "(".implode(",",$bCols[$i]).")";
        }
        // If there is no bind array (errors in file array)
        // just return false
        if(empty($bind))
            return false;
        // assign bind
        $return['bind'] =   $bind;
        // Implode rows
        $return['cols'] =   implode(",",$iCols);
        // return the final data array
        return $return;
    }

使用:

    // Make sure to include the above functions here....

    // Get the uploads
    $uploads    =   CompileUpload();
    // If there are uploads and the user is logged in
    if(!empty($uploads) && !empty($_SESSION['id'])) {
            // Is this really correct? Do you have a table for each user?
            // Compile your statement
            $statement  =   "INSERT into `".$_SESSION['id']."` (`immagine`,`type`,`profilo`) VALUES ".$uploads['cols'];
            // Get connection and prepare
            // You may need to do $con = connection(); $con->prepare...etc.
            // but this should work
            $query      =   connection()->prepare($statement);
            // Execute with bind values
            $query->execute($uploads['bind']);
        }

sql语句看起来像这样:

INSERT into `whatever` (`immagine`,`type`,`profilo`) VALUES (:0name,:0type,:0data)

多次上传将是:

INSERT into `whatever` (`immagine`,`type`,`profilo`) VALUES (:0name,:0type,:0data),(:1name,:1type,:1data)