[英]Store multiple uploads by destination folder on MySQL DB
I saw a bunch of question similar to mine but not quite the same, in a way that I would have to change the way I'm currently storing the files on DB. 我看到了一堆与我的问题类似但又不完全相同的问题,以某种方式,我将不得不更改当前在DB上存储文件的方式。
I have this one form on the first file (getPic.php): 我在第一个文件(getPic.php)上有以下表格:
<form enctype="multipart/form-data" method="POST" action="sendPics.php">
<font size=4 color=DarkBlue><b>Choose 5 photos of your pet:</b></font><br><br>
<input type="file" name="file[]" multiple="multiple" /><br><br>
<input name="send" type="submit" value="Upload photos">
And this on the other (sendPics.php) 与此相关的其他内容(sendPics.php)
$directory = "Images/";
if(!is_dir($directory)){
echo "Folder does not exist";
}else{
$file = isset($_FILES['file']) ? $_FILES['file'] : FALSE;
for ($control = 0; $control < count($file['name']); $control++){
$destiny = $directory."/".$file['name'][$control];
if(move_uploaded_file($file['tmp_name'][$control], $destiny)){
echo "Upload successful<br>";
}else{
echo "Error uploading the file";
}
}
}
$destiny = "/".$destiny;
My table in MySQL looks like this: 我在MySQL中的表如下所示:
Field | Type
----------------+------------
idPetsPics | int(11)
linkProfilePic | varchar(60)
linkPhoto1 | varchar(60)
linkPhoto2 | varchar(60)
linkPhoto3 | varchar(60)
linkPhoto4 | varchar(60)
Pet_idPet | int(11) //(FK with PetsID on Pet's table)
So I need to store the path of the pics uploaded. 因此,我需要存储上传照片的路径。 Currently I'm only able to store the first one on the DB, though all the 5 pics gets to the destination folder successfully.
目前,我只能将第一个存储在DB上,尽管所有5张照片都已成功到达目标文件夹。 For now I have:
现在,我有:
$petID = $_SESSION['petID']; // obtained from a third file (queries to insert date into Pet)
require_once('dbConnection.php');
$insertPetPic = "INSERT INTO petPic (idPetsPics, linkProfilePic, Pet_idPet) VALUES (NULL, '$destiny', $petID[0])";
mysqli_query($conn, $insertFotoPet);
How can I change my code to store all the 5 paths? 如何更改代码以存储所有5条路径?
For each image that was moved successfully, you need to store the path to the image somewhere, for example in a $paths
array: 对于成功移动的每个图像,您需要将图像的路径存储在某个位置,例如在
$paths
数组中:
$directory = "Images/";
$paths = array();
if(!is_dir($directory)){
echo "Folder does not exist";
}else{
$file = isset($_FILES['file']) ? $_FILES['file'] : FALSE;
for ($control = 0; $control < count($file['name']); $control++){
$destiny = $directory."/".$file['name'][$control];
if(move_uploaded_file($file['tmp_name'][$control], $destiny)){
$paths[] = $destiny;
echo "Upload successful<br>";
}else{
echo "Error uploading the file";
}
}
}
And then in your query you use the paths from the array: 然后在查询中使用数组中的路径:
$insertPetPic = "INSERT INTO petPic (idPetsPics, linkProfilePic, linkPhoto1, linkPhoto2, linkPhoto3, linkPhoto4, Pet_idPet) VALUES (NULL, '$paths[0]', '$paths[1]', '$paths[2]', '$paths[3]', '$paths[4]', $petID[0])";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.