从单个文件输入将多个图像上载到数据库 [英] Upload multiple images to a database from a singe fileinput

查看:102
本文介绍了从单个文件输入将多个图像上载到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一次将多个图片上传到我的数据库。我能用单个文件做到这一点,但我无法使用html5多项选项。

I'm trying to upload multiple images at once to my database. I was able to do it with single files but i'm not able to get it going with the html5 multiple option.

这是我的表格:

<form action="includes/saveImage.php" method="POST" enctype="multipart/form-data">
    File:
    <input type="file" multiple="multiple" name="image[]" accept="image/*"> <input type="submit" value="upload">
</form>

这是我的saveImage.php

and this is my saveImage.php

$files = array();
$fdata = $_FILES["image"];
if (is_array($fdata["name"]))
        for ($i = 0; $i < count($fdata['name']); ++$i) {
                $files[] = array(
                    'name' => $fdata['name'][$i],
                    'tmp_name' => $fdata['tmp_name'][$i],
                    'image' =>  file_get_contents($fdata['tmp_name'][$i]),
                );
        }
} else {
$files[] = $fdata;
}

foreach ($files as $file) 
{
    if (!$album_mysqli->query("INSERT INTO '1' VALUES ('','{$file['name']}','{$file['image']}',NOW())")) 
        {
            print"error while uploading";
        }
   // print_r( $file );
}

如果我取消注释最后一个print_r($ file);它显示了图像文件的二进制数据。
为什么它不会上传到我的数据库的任何想法?

If i uncomment the last print_r($file); it shows the binary data of the image file. Any ideas why it won't upload to my database?

我想写的数据库使用BLOB字段作为图像。

The database I want to write to uses a BLOB field for the image.

解决了我的问题

SQL语句有问题
添加这样的图片效果很好...

There was some problem with the SQL-statement Adding the pictures in this way works just fine...

    foreach ($files as $file) 
    {
        $image = file_get_contents($file['tmp_name']);
        $result = $album_mysqli->prepare("INSERT INTO '1' VALUES ('', ?, ?, CURDATE())");
        $result->bind_param("ss", $file['name'], $image);
        if (!$result->execute()) 
        {
            echo"Problem on file {$file['name']}. <br/>";
        }
        else
        {
            echo("{$file['name']} was added to the database. <br/>");
        }

        $result->close();
    }


推荐答案

解决了我的问题

SQL语句有问题
以这种方式添加图片效果很好......

There was some problem with the SQL-statement Adding the pictures in this way works just fine...

$files = array();
$fdata = $_FILES["image"];
if (is_array($fdata["name"]))
    for ($i = 0; $i < count($fdata['name']); ++$i) {
            $files[] = array(
                'name' => $fdata['name'][$i],
                'tmp_name' => $fdata['tmp_name'][$i],
                'image' =>  file_get_contents($fdata['tmp_name'][$i]),
            );
    }
} else {
$files[] = $fdata;
}

    foreach ($files as $file) 
    {
        $image = file_get_contents($file['tmp_name']);
        $result = $album_mysqli->prepare("INSERT INTO '1' VALUES ('', ?, ?, CURDATE())");
        $result->bind_param("ss", $file['name'], $image);
        if (!$result->execute()) 
        {
            echo"Problem on file {$file['name']}. <br/>";
        }
        else
        {
            echo("{$file['name']} was added to the database. <br/>");
        }

        $result->close();
    }

这篇关于从单个文件输入将多个图像上载到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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