2014-12-19 64 views
3

這裏是一個腳本,用於上傳服務器文件夾中的多個圖像並存儲它們在數據庫中的路徑,但是圖像的路徑只能存儲在一列中。保存數據庫中服務器文件夾上傳的圖像的路徑

例如:我上傳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); 
?> 

將不勝感激,如果有人可以幫助我

+1

**警告**:使用'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

+1

您正在編寫一個SQL查詢,指出「SET offimage = blah」,當然它只會添加到該列。 PHP不能僅僅猜測你想要的列名,你需要告訴它你的查詢中的列。但在你這樣做之前,試着想想會發生什麼,如果他們想要上傳5個圖像,或10,或100 ... – miken32 2014-12-19 04:38:27

+0

@ miken32,我打算將上傳數量限制爲最大5的圖像,在SQL查詢我只使用了一列,因爲我無法理解如何分離$ filepath中的圖像以便保存在不同的列中,請問如何才能完成 – jane 2014-12-19 05:33:00

回答

0

之所以要三個圖像的路徑信息看起來像一個大的字符串:

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表。

相關問題