【问题标题】:Receiving a Fatal Error on multiple file upload [duplicate]在多个文件上传时收到致命错误[重复]
【发布时间】:2016-01-03 12:38:44
【问题描述】:

我已经制作了一个脚本来使用表单上传多个文件:

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

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

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
$where = dirname(__FILE__);
include($where . "/config/db.php");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

foreach ($_FILES as $file) {   
    $nome_file_temporaneo = $file["tmp_name"];
    $nome_file_vero = $file["name"];
    $tipo_file = $file["type"];
    $not_profilo = '1';

    for($i=0;$i<sizeof($tipo_file);$i++) {
        $dati_file = file_get_contents($nome_file_temporaneo[$i]);
        $query = "INSERT INTO ".$_SESSION['id']." (immagine,type,profilo) values (?,?,?)";

        $stmt = $dbh->prepare($query);
        $stmt->bindParam(1, $dati_file, PDO::PARAM_LOB);
        $stmt->bindParam(2, $tipo_file[$i],PDO::PARAM_STR);
        $stmt->bindParam(3, $not_profilo, PDO::PARAM_STR);
        $stmt->execute();
    }
}
header("location: profile_set.php");
?>

这给了我一个错误:

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

第 24 行包含:$stmt-&gt;execute()

任何帮助将不胜感激。

【问题讨论】:

  • 如果我每次敦促人们避免在他们的数据库中存储文件时都有一分钱,我可以接管 oracle。所以再来一次。不要将文件内容保存在数据库中。
  • 致命错误的其余部分是什么?致命错误是什么?

标签: php mysql image file upload


【解决方案1】:

尝试使用插入-&gt;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)

【讨论】:

  • 感谢您的回答 Resclatt,代码工作,部分.. 现在的问题是我在 upload_image.php 页面中没有任何错误,并且发送到 db 的所有文件只有 1B大小..
  • 执行print_r($uploads); 以查看上传数据是否正确填充
  • 对不起,这是我的代码问题,不是你的...我将 'profilo' 存储为 'immagine' 列,所以它只在 blob 列中插入了 '1'。跨度>
  • 听起来不错。如果有效,请务必将问题标记为已回答。
猜你喜欢
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
  • 2018-07-06
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多