[英]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.