用PDO添加多个文件 [英] Adding multiple files with PDO

查看:163
本文介绍了用PDO添加多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为 gallery.php 的页面中有一个表单,代码如下:

 < form action =upload_image.phpmethod =POSTclass =form-horizo​​ntalenctype =multipart / form-data> 
< div class =form-group>
< div class =col-md-12>
< div align =center>< a href =#class =btn btn-defaultid =image_alt>< span class =glyphicon glyphicon-ok> < /跨度> & nbspSeleziona immagini< / a>< / div>
< input id =imagename =files []type =filemultiple =multiplerequired =requiredstyle =margin-left:50%; class =btn btn-default>
< / div>
< / div>
< div class =form-group>

接下来,我有一个名为upload_image.php的页面,代码如下:

 <?php 
session_start();
$ where = dirname(__ FILE__);
include($ where。/config/db.php);
$ categoria_img ='1';
$ id_album ='1';

//循环$ _FILES来执行所有文件
foreach($ _FILES as $ file){
$ nome_file_temporaneo = $ file [tmp_name];
$ nome_file_vero = $ file [name];
$ tipo_file = $ file [type];
($ i = 0; $ i< sizeof($ tipo_file); $ i ++){
echo< br> Nome:。 $ nome_file_temporaneo [$ i]于 <峰; br> 中。
$ dati_file = file_get_contents($ nome_file_temporaneo [$ i]);
$ query =INSERT INTO images(category_id,image_big,image_type,id_album)values(:categoria_img,:data,:img_type,:id_album);
echo< br>。$ query;
$ stmt = $ dbh-> prepare($ query);

$ stmt-> bindParam(:categoria_img,$ categoria_img,PDO :: PARAM_INT);
$ stmt-> bindParam(:data,$ dati_file,PDO :: PARAM_STR);
$ stmt-> bindParam(:img_type,$ tipo_file [$ i],PDO :: PARAM_STR);
$ stmt-> bindParam(:id_album,$ id_album,PDO :: PARAM_INT);

$ stmt-> execute();
}
}
?>

问题在于 upload_image.php doesn 't显示任何错误..任何东西都被存储在数据库中通过 gallery.php 页面中的表单传递文件之后..有人可以帮我吗?



UPDATE


致命错误:在C:\xampp\htdocs\castellazzo_theme\PHP中带有消息SQLSTATE [HY093]:无效参数编号:绑定变量数与令牌数不匹配的未捕获异常'PDOException' Version \upload_image.php:31 Stack trace:#0 C:\xampp\htdocs\castellazzo_theme\PHP Version\upload_image.php(31):PDOStatement-> execute()#1 {main}抛出C:\xampp\htdocs\castellazzo_theme\PHP版本\upload_image.php on line 31



解决方案

  $ stmt-> bindParam(:data,$ dati_file,PDO :: PARAM_STR); 

应该是

  $ stmt-> bindParam(:data,$ dati_file,PDO :: PARAM_LOB); 

请参阅 Manual

但是你应该存储图像的位置,而不是将其存储为Blob。 $ b

I have a form in a page called gallery.php, and the code is the following:

<form action="upload_image.php" method="POST" class="form-horizontal" enctype="multipart/form-data">
    <div class="form-group">
        <div class="col-md-12">
            <div align="center"><a href="#" class="btn btn-default" id="image_alt"><span class="glyphicon glyphicon-ok"></span> &nbspSeleziona immagini</a></div>
            <input id="image" name="files[]" type="file" multiple="multiple" required="required" style="margin-left:50%;" class="btn btn-default">
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-4 control-label" for="btn_add_img"></label>
        <div class="col-md-12">
            <div align="center">
                <button id="btn_add_img" name="btn_add_img" class="btn btn-primary">Carica</button>
            </div>
        </div>
    </div>
</form>

Next, i have the page called upload_image.php with the following code:

<?php
session_start();
$where = dirname(__FILE__);
include($where . "/config/db.php");
$categoria_img = '1';
$id_album = '1';

// Loop $_FILES to exeicute all files
foreach ($_FILES as $file) {   
    $nome_file_temporaneo = $file["tmp_name"];
    $nome_file_vero = $file["name"];
    $tipo_file = $file["type"];
    for($i=0;$i<sizeof($tipo_file);$i++) {
        echo "<br>Nome:" . $nome_file_temporaneo[$i]."<br>";
        $dati_file = file_get_contents($nome_file_temporaneo[$i]);
        $query = "INSERT INTO images (category_id,image_big,image_type,id_album) values (:categoria_img,:data,:img_type,:id_album)";
        echo "<br>".$query;
        $stmt = $dbh->prepare($query);

        $stmt->bindParam(":categoria_img", $categoria_img, PDO::PARAM_INT);
        $stmt->bindParam(":data", $dati_file, PDO::PARAM_STR);
        $stmt->bindParam(":img_type", $tipo_file[$i],PDO::PARAM_STR);
        $stmt->bindParam(":id_album", $id_album, PDO::PARAM_INT);

        $stmt->execute();
    }
}
?>

The problem is that upload_image.php doesn't show any error..and anything be stored in the database after passing file by the form in gallery.php page..could someone help me?

UPDATE

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in C:\xampp\htdocs\castellazzo_theme\PHP Version\upload_image.php:31 Stack trace: #0 C:\xampp\htdocs\castellazzo_theme\PHP Version\upload_image.php(31): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\castellazzo_theme\PHP Version\upload_image.php on line 31

解决方案

$stmt->bindParam(":data", $dati_file, PDO::PARAM_STR);

Should be

$stmt->bindParam(":data", $dati_file, PDO::PARAM_LOB);

See Manual

But you should be storing location of image rather than storing it a Blob.

这篇关于用PDO添加多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆