简体   繁体   English

如何将多个文件上传名称保存到数据库?

[英]How can i save multiple file upload name to a database?

i know similar question have been asked here but my problem is that i have not seen the one that has an answer to my,i want users to upload multiple photos on one post,so my problem is do i have to add more rows to my database to store each file name and if so what if i have 10 rows and user wish to upload 11 photos what will happen,am just wondering how facebook made that possible 我知道在这里有人问过类似的问题,但是我的问题是我还没有看到能回答我的问题,我希望用户在一张帖子上上传多张照片,所以我的问题是我是否必须向我的行中添加更多行数据库来存储每个文件名,如果是的话,如果我有10行并且用户希望上传11张照片该怎么办,我只是想知道Facebook如何使这成为可能

if(count($_FILES['uploads']['filesToUpload'])) {
    foreach ($_FILES['uploads']['filesToUpload'] as $file) {

        //do your upload stuff here
        echo $file;

    }
}

i can upload multiple photos and move to folder but how can i store all their names in my db 我可以上传多张照片并移至文件夹,但如何将所有照片名称存储在数据库中

So if i'm understanding right, you just have to get the id of the current user that is logged in and then have a photos table which contain at least a path of the photo and the id of the user as foreign key. 因此,如果我理解正确,则只需获取已登录的当前用户的id ,然后拥有一个photos表,该表至少包含photos的路径和作为外键的用户ID。 this way you can add as many photo's for a single user as you want. 这样,您可以为单个用户添加任意​​数量的照片。

This would be, like you realised, a bad idea. 就像您意识到的那样,这是个坏主意。 You should add an other table for the photos. 您应该为照片添加其他表格。

In that table, you store one photo with it's post id in each row. 在该表中,每行存储一张带有其帖子ID的照片。

You should take a look at 3NF . 您应该看一下3NF

Let me know if you need more help. 让我知道您是否需要更多帮助。

This is an example, you just adapt it for yourself. 这是一个示例,您只需对其进行调整即可。

<form action="file_reciever.php" enctype="multipart/form-data" method="post">
<input type="file" name="files[]" multiple/>
<input type="submit" name="submission" value="Upload"/>
</form>

the PHP goes (file_reciever.php): PHP去(file_reciever.php):

<?php
    if (isset($_POST['submission'] && $_POST['submission'] != null) {
        for ($i = 0; $i < count($_FILES['files']['name']); $i++) {
            //Get the temp file path
            $tmpFilePath = $_FILES['files']['tmp_name'][$i];

            //Make sure we have a filepath
            if ($tmpFilePath != "") {
                //Setup our new file path
                $newFilePath = "./uploadFiles/" . $_FILES['files']['name'][$i];

                //Upload the file into the temp dir
                if (move_uploaded_file($tmpFilePath, $newFilePath)) {

                    //Handle other code here

                }
            }
        }
    }
?>

I hope it helps. 希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在mysql数据库中保存多个文件名(多个上传)? - How to save multiple file name (multiple upload) in mysql database? 当我上载docx文件时,内容存储在数据库中如何保存图像并在数据库中插入图像名称? - when i have upload docx file, content are storing in database How can i save image and insert image name in database? 如何将多个上传文件保存到数据库 - How to save multiple upload file to the database 如何将多个文件名保存到数据库? - How to save multiple file name to a database? 如何将多个文件名保存到数据库? - How to save multiple file name to database? 重命名文件以上传后,Codeigniter如何将新文件名保存到数据库中 - Codeigniter after renaming file for upload how do i save the new file name to my database Laravel 5.2 + 上传文件并在数据库中保存名称 - Laravel 5.2 + upload file and save name in database 如何将base64字符串转换为图像并将文件上传到存储中并将路径保存在数据库中 - How can I convert base64 string to image and upload the file in storage and save the path in the database 我如何仅将文件名保留为VARCHAR就可以在数据库中上传照片? - How can i upload a photo in a database with just holding on the the file name as a VARCHAR? 如何将多个php文件上传的单个文件名添加到数据库? - How can i add the individual file names of a multiple php file upload to the database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM