這裏是一個腳本,用於上傳服務器文件夾中的多個圖像並存儲它們在數據庫中的路徑,但是圖像的路徑只能存儲在一列中。保存數據庫中服務器文件夾上傳的圖像的路徑
例如:我上傳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);
?>
將不勝感激,如果有人可以幫助我
**警告**:使用'mysqli'時,您應該使用參數化查詢和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢。 **不要**使用字符串插值來實現此目的,因爲您將創建嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 – tadman 2014-12-19 04:14:06
您正在編寫一個SQL查詢,指出「SET offimage = blah」,當然它只會添加到該列。 PHP不能僅僅猜測你想要的列名,你需要告訴它你的查詢中的列。但在你這樣做之前,試着想想會發生什麼,如果他們想要上傳5個圖像,或10,或100 ... – miken32 2014-12-19 04:38:27
@ miken32,我打算將上傳數量限制爲最大5的圖像,在SQL查詢我只使用了一列,因爲我無法理解如何分離$ filepath中的圖像以便保存在不同的列中,請問如何才能完成 – jane 2014-12-19 05:33:00