[英]save the path of image being uploaded on server folder in database
这是一个脚本,用于在服务器文件夹上上传多个图像并在数据库中存储它们的路径,但是图像的路径只存储在一列中。
例如:我上传了3张图片,image1.jpg,image2.jpg,image3.jpg。 这些图像应该存储在3列中,即offimage1,offimage2,offimage3。
现在的问题是所有3个图像的路径仅存储在offimage1下。 存储在offimage1中的路径看起来像这样,
uploads/image1.jpg*uploads/image1.jpgimage2.jpg*uploads/image1.jpgimage2.jpgimage3.jpg
我希望图像应该以这种方式存储:
uploads/image1.jpg in colum offimage1
uploads/image1.jpgimage2.jpg in colum offimage2
uploads/image1.jpgimage2.jpgimage3.jpg in colum offimage3
HTML表单
<form enctype="multipart/form-data" action="insert_image.php?id=<?php echo $_GET['id']; ?>" method="post">
<div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>
<input type="button" id="add_more" class="upload" value="Add More Files"/>
<input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
</form>
insert_image.php
<?php
ob_start();
require 'connection.php';
if (isset($_POST['submit'])) {
$j = 0; //Variable for indexing uploaded image
$target_path = "uploads/"; //Declaring Path for uploaded images
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed
$ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
$file_extension = end($ext); //store extensions in the variable
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];//set the target path with a new name of image
$j = $j + 1;//increment the number of uploaded images according to the files in array
if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
//echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
$file_name_all.=$target_path."*";
$filepath = rtrim($file_name_all, '*');
//echo $filepath;
$officeid = $_GET['id'];
$sql = "UPDATE register_office SET offimage='$filepath' WHERE id='$officeid' ";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
} else {//if file was not moved.
echo $j. ').<span id="error">please try again!.</span><br/><br/>';
}
} else {//if file size and file type was incorrect.
echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
header("Location: co_request_sent.php ");
}
mysqli_close($con);
?>
如果有人能帮助我,我将不胜感激
您的所有三个图像的路径信息看起来像一个大字符串的原因:
uploads/image1.jpg*uploads/image1.jpgimage2.jpg*uploads/image1.jpgimage2.jpgimage3.jpg
是因为你循环遍历FILES数组并在每次迭代时连接$ target_path:
$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1];
如果您希望$ target_path仅保存当前图像的信息,请首先通过移动来重置变量:
$target_path = "uploads/";
在你的for循环中(所以向下移动两行)。
这是你问题的第一部分。 但是,在名为offimage1,offimage2等的列中存储有关图像的信息是一个非常糟糕的主意 。
这种事情应该存储在一行,而不是列。
所以也许你最好用一个名为“id”,“officeid”和“path”的列创建一个名为offimages的新表。
现在只需为每个图像插入一行(现在无论你有多少都没关系),并确保使用officeid将其链接到register_office表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.