0
這是一個上傳多張圖片的腳本。如果我上傳3張圖像,然後3圖像獲取存儲在服務器的文件夾中,但在數據庫中的每個圖像路徑被存儲兩次,從而得到6項,而不是3項上傳圖片在數據庫中的存儲路徑
<?php
ob_start();
include('co_session.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'];
echo $officeid;
$str= $filepath;
$array = explode('*', $str);
foreach ($array as $item)
{
$sql="INSERT INTO officeimage (offimage,officeid,lat,lon) VALUES ('$item','$officeid','$user_latitude','$user_longitude')";
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);
?>
任何人都可以指出錯誤
做一個$ filepath打印;你有多少數據? – 2014-12-19 08:45:16
@Marco Mura我得到了上傳圖片的路徑,例如,如果我上傳image1,image2和image3我得到的路徑: - uploads/image1.jpeguploads/image1.jpeg * uploads/image1.jpegimage2.jpguploads/image1.jpeg * uploads/image1.jpegimage2.jpg * uploads/image1.jpegimage2.jpgimage3.jpg – sam 2014-12-19 08:55:39
上傳文件後做插入循環。在時間只得到1,並在時間只插入1 – 2014-12-19 08:57:49