2017-05-05 50 views
0

這是我的表單頁面如何在php中一次上傳多個圖像?

<form action="gallerycontroller.php" method="post" enctype="multipart/form-data"> 
<div> 
<label class="desc" id="title4" for="Field4"> 
    Message 
</label>      
<div> 
<textarea id="reply" name="name" spellcheck="true" rows="10" cols="50" tabindex="4"></textarea>     
          <!-- Custom icons with callback --> 
          <input type="file" name="file"> 

          </div> 
          </div> 

          <div> 

          <div> 
           <div> 
           <input id="saveForm" name="saveForm" type="submit" value="Save"> 
          </div> 
          </div> 

的代碼,這是

<?php 
include("model.php"); 
$db = new database; 
$name = $_POST['name']; 
$file = $_FILES['file']; 
$filename = $file['name']; 
$filetmp = $file['tmp_name']; 
$file_ext = explode('.',$filename); 
$file_ext = strtolower(end($file_ext)); 
$file_name_new = uniqid('',true) .'.'. $file_ext; 
$file_destination = 'file/' .$file_name_new; 
move_uploaded_file($filetmp, $file_destination); 


     $designation=$_POST['principal']; 
     $r=$db->insert($name,$file_destination); 
     if($r>0) 
    { 
      header("Location:upload.php?reply='sucess'"); 
    } 
    else 
    { 
     header("Location:upload.php?reply='fail'");  
    } 
?> 

gallerycontroller頁面,但我想在同一時間單個文件上傳的這段代碼工作上傳多個文件很好。如果可能,請將此代碼轉換爲多文件上傳,否則請參閱新代碼。

回答

0

使用 「多重」 的文件

<input type="file" name="img[]" multiple> 

然後在PHP中使用的循環屬性

$loop = 0; 
foreach($_FILES['file']['name'] as $file){ 
    $filename = $file['name'][$loop]; 
    $filetmp = $file['tmp_name'][$loop]; 
    $file_ext = explode('.',$filename); 
    $file_ext = strtolower(end($file_ext)); 
    $file_name_new = uniqid('',true) .'.'. $file_ext; 
    $file_destination = 'file/' .$file_name_new; 
    move_uploaded_file($filetmp, $file_destination); 
    $loop++; 
}