使用PHP进行多个图像上传,仅将一个文件路径保存到MySQL数据库 [英] Multiple image upload with PHP saving only one file path to MySQL Database

查看:187
本文介绍了使用PHP进行多个图像上传,仅将一个文件路径保存到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为朋友建立网站,并设置了一个将多个图片上传到数据库的表单。它只能被一个人访问,并且对于公众不可见。为了方便编码,我忽略了对代码的许多可能的威胁,以便让它工作。

I am building a site for a friend and have set up a form that will upload multiple images to the database. It will only be accessed by one person, and is not visible to the general public so for sake of easy coding I have ignored a lot of possible threats to the code in order to just get it working.

HTML很简单,只是:

The HTML is simple it's just:

<form action="(url here)" method="post" enctype="multipart/form-data">
     <input type="file" name="files[]" multiple>
     <input id="formbutton" type= "submit" value="Upload!">
</form>

这是我担心的php。

if(isset($_FILES['files'])){ 
    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name){
        $target="images/costume/";
        $target=$target.$_FILES['files']['name'][$key];
            if(move_uploaded_file($tmp_name, $target)){
                $id = mysql_insert_id($con);
                mysql_query("INSERT INTO costumes (id,name) VALUES ('$id','$name')");
            }
    }
}

到服务器上的目录,但只有一个图像实际存储到MySQL表作为一个ID和文件名的行。

All of the image files upload to the directory on the server, but only ONE of the images actually stores into the MySQL table as a row with ID and file name.

我一直在欺骗我的大脑和搜索疯狂的解决方案,但没有任何工作。我知道它与foreach循环有关,但我失去了做什么,使我上传的每一个图像在数据库中创建一行。

I've been racking my brain and searching like mad for a solution, but there aren't any that have worked. I know it has something to do with the foreach loop but I'm at a loss of what to do to make every image I upload create a row in the database.

感谢您的帮助。

对不起,我正在进行更改,忘记切换回最后一部分的原始代码:

Sorry, I was making changes and forgot to switch back to the original code for the last section:

if(isset($_FILES['files'])){ 
    foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name){
        $target="images/costume/";
        $target=$target.$_FILES['files']['name'][$key];
            if(move_uploaded_file($tmp_name, $target)){
                $id = mysql_insert_id($con);
                mysql_query("INSERT INTO costumes (id,name) VALUES ('$id','$target')");
            }
    }
}

推荐答案

1-您应该使用要存储的图像列表创建数组

1- You should make an array with the list of image you want to stored

2-在表格中的列ID中使用AutoIncrement(重要

2- Use AutoIncrement in your column Id in the table(Important)

也可以使用数组查询如下:

also you have the array you can do the query as follow:

捕获来自帖子的数据

$IMG = isset($_POST['files']) ? $_POST['files'] : array();
if (!empty($IMG)) {
    $uploads_dir = 'images/costume/';
    foreach ($IMG["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $IMG["tmp_name"][$key];
            $name = $IMG["name"][$key];
            move_uploaded_file($tmp_name, "$uploads_dir/$name");
            $name_array=mysql_real_escape_string($name);
            $value_insert[] = "('" . $name_array . "')";
        }
    }
    $values_insert = implode(',', $value_insert);
    $query = "INSERT INTO costumes (name) VALUES" . $values_insert;
    $result = mysql_query($query);
}else{
 echo 'empty array';
}

As side注:
Mysql_ *扩展名已弃用从PHP 5.5.0开始,并将在将来删除。

有用的链接

A useful link Why shouldn't I use mysql_* functions in PHP

这篇关于使用PHP进行多个图像上传,仅将一个文件路径保存到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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