2014-12-19 98 views
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); 
?> 

任何人都可以指出錯誤

+0

做一個$ filepath打印;你有多少數據? – 2014-12-19 08:45:16

+0

@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,並在時間只插入1 – 2014-12-19 08:57:49

回答

0

我建議聲明外部循環的「常數」如$validextensions。 而你聲明$file_extension但下一行你做$ext[count($ext) - 1]

對於你多重插入的問題:你做一個foreach循環插入for循環移動它們的每個元素。

Loop #1 
    Move image #1 
    Add image #1 to Stack 
    Subloop 
     Insert image #1 from Stack 
Loop #2 
    Move image #2 
    Add image #2 to Stack 
    Subloop 
     Insert image #1 from Stack 
     Insert image #2 from Stack 
Loop #3 
    Move image #3 
    Add image #3 to Stack 
    Subloop 
     Insert image #1 from Stack 
     Insert image #2 from Stack 
     Insert image #3 from Stack 

在結束:6個條目

:插入三次
  • 圖像#2插入兩次
  • 圖像#3插入一次
  • TOTAL

    • 圖像#1

      將廁所移到廁所外面p或刪除堆棧/ foreach以在每個循環中插入當前圖像。

    相關問題