2015-10-06 69 views
-1

我做了一個腳本使用形式上傳多個文件:接收多個文件中的致命錯誤上傳

<form action="upload_image.php" id="form_img" method="POST" enctype="multipart/form-data"> 
    <div align="center"> 
     <div class="fileUpload btn btn-primary"> 
      <span>Carica immagini nella galleria</span> 
      <input type="file" name="immagini[]" multiple="multiple" id="file_img" class="upload"/> 
      <script> 
      document.getElementById("file_img").onchange = function() { 
       document.getElementById("form_img").submit(); 
      }; 
      </script> 
     </div> 
    </div> 
</form> 

的JavaScript代碼應該提交表單時,用戶已經選擇了一個文件,這裏是PHP我使用的處理上載:

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 
session_start(); 
$where = dirname(__FILE__); 
include($where . "/config/db.php"); 
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

foreach ($_FILES as $file) { 
    $nome_file_temporaneo = $file["tmp_name"]; 
    $nome_file_vero = $file["name"]; 
    $tipo_file = $file["type"]; 
    $not_profilo = '1'; 

    for($i=0;$i<sizeof($tipo_file);$i++) { 
     $dati_file = file_get_contents($nome_file_temporaneo[$i]); 
     $query = "INSERT INTO ".$_SESSION['id']." (immagine,type,profilo) values (?,?,?)"; 

     $stmt = $dbh->prepare($query); 
     $stmt->bindParam(1, $dati_file, PDO::PARAM_LOB); 
     $stmt->bindParam(2, $tipo_file[$i],PDO::PARAM_STR); 
     $stmt->bindParam(3, $not_profilo, PDO::PARAM_STR); 
     $stmt->execute(); 
    } 
} 
header("location: profile_set.php"); 
?> 

這給了我一個錯誤:

Fatal error: in C:\xampp\htdocs\tp\upload_image.php on line 24

第24行是一個包含行:$stmt->execute()

任何幫助,將不勝感激。

+0

如果我有時間的每一分錢我呼籲人們避免在其數據庫中存儲的文件,我可以收購甲骨文。再來一次。不要保存數據庫中的文件內容。 – e4c5

+0

致命錯誤的其餘部分說的是什麼?致命錯誤是什麼? – Rasclatt

回答

0

嘗試使用插入->execute(array())陣列結合。如果你想確保它們應該是什麼值,只需在foreach()循環中做一些驗證。最後一件事,你說你的形式做多上傳,但你只有一個輸入和你有它儘快上傳隨着輸入的變化,所以這是一點點困惑:

// I am just saving your connection to a function just to clean it up a bit 
function connection() 
    { 
     include(__DIR__."/config/db.php"); 
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     return $dbh; 
    } 
// I like to reogranize my $_FILES array so each file is in it's own array 
function organize($array = false) 
    { 
     if(!is_array($array) || empty($array)) 
      return $array; 

     foreach($array['name'] as $key => $value) { 

       if($array['error'][$key] != 0) { 
         $files[$key] = false; 
         continue; 
        } 

       $files[$key] = array(
              "name"  => $array['name'][$key], 
              "tmp_name" => $array['tmp_name'][$key], 
              "type"  => $array['type'][$key], 
              "error"  => $array['error'][$key], 
              "size"  => $array['size'][$key] 
             ); 
      } 

     return $files; 
    } 

// This will return an array of bind values and statement values 
function CompileUpload($use_name = 'immagini') 
    { 
     // If empty, do nothing 
     if(empty($_FILES[$use_name])) 
      return false; 

     //Reorganize array 
     $FILES = organize($_FILES[$use_name]); 
     $return = false; 
     foreach ($FILES as $i => $file) { 
      if($file["error"] !== 0) 
       continue; 
      // I would suggest just saving the name and location of 
      // the file(s) instead of saving them to the database. 
      $temp = $file["tmp_name"]; 
      $name = $file["name"]; 
      $type = $file["type"]; 
      $data = file_get_contents($temp); 
      // Create a bind array 
      $bind[":".$i."name"] = $name; 
      $bind[":".$i."type"] = $type; 
      $bind[":".$i."data"] = $data; 
      // Create the append values for the sql statement 
      $bCols[$i][] = ":".$i."name"; 
      $bCols[$i][] = ":".$i."type"; 
      $bCols[$i][] = ":".$i."data"; 
      // Implode and save to a master row array 
      $iCols[]  = "(".implode(",",$bCols[$i]).")"; 
     } 
     // If there is no bind array (errors in file array) 
     // just return false 
     if(empty($bind)) 
      return false; 
     // assign bind 
     $return['bind'] = $bind; 
     // Implode rows 
     $return['cols'] = implode(",",$iCols); 
     // return the final data array 
     return $return; 
    } 

使用方法:

// Make sure to include the above functions here.... 

    // Get the uploads 
    $uploads = CompileUpload(); 
    // If there are uploads and the user is logged in 
    if(!empty($uploads) && !empty($_SESSION['id'])) { 
      // Is this really correct? Do you have a table for each user? 
      // Compile your statement 
      $statement = "INSERT into `".$_SESSION['id']."` (`immagine`,`type`,`profilo`) VALUES ".$uploads['cols']; 
      // Get connection and prepare 
      // You may need to do $con = connection(); $con->prepare...etc. 
      // but this should work 
      $query  = connection()->prepare($statement); 
      // Execute with bind values 
      $query->execute($uploads['bind']); 
     } 

SQL語句會是這個樣子:

INSERT into `whatever` (`immagine`,`type`,`profilo`) VALUES (:0name,:0type,:0data) 

多上傳爲:

INSERT into `whatever` (`immagine`,`type`,`profilo`) VALUES (:0name,:0type,:0data),(:1name,:1type,:1data) 
+0

感謝您的回答Resclatt,代碼的工作,部分..現在的問題是,我沒有在upload_image.php頁的所有錯誤,所有發往數據庫中的文件只有大小1B .. –

+0

待辦事項'的print_r($上傳);',看它是否與上傳數據 – Rasclatt

+0

對不起,這是礦山代碼中的問題,不是你的......我是存儲「PROFILO」爲「想象,」列,所以只插入正確填充'blob'列中的'1'。 –