2017-01-18 57 views
0

我想在一個窗體上可以插入文件的多個字段。php - 命名窗體上的多個文件輸入字段

我有下面的腳本,但我想能夠確定插入的文件屬於保存的名稱字段。

<form action="" method="post" enctype='multipart/form-data' id="form" name="form"> 

    Input 1<input type="file" name="upload[]" > 
    Input 2<input type="file" name="upload[]" > 
    Input 3<input type="file" name="upload[]" > 

    <button id="submit-button">Upload</button> 

</form> 


<?php 

//if(isset($_POST['submit']) && !empty($_POST)){ 
$count = 0; 
$max_file_size = 5000000; 
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ 
    // Loop $_FILES to exeicute all files 

    foreach ($_FILES['upload']['name'] as $f => $name) { 

     $path = 'documents'; //path of directory 

     if ($_FILES['upload']['error'][$f] == 4) { 
      continue; // Skip file if any error found 
     } else { 
      if ($_FILES['upload']['size'][$f] > $max_file_size) { 
       $message[] = "$name is too large!."; 
       continue; // Skip large files 
      } 
      else { 
       // No error found! Move uploaded files 
       //$name_of_file = $_FILES['username']['name'][$f]; 
       $temp_name = $_FILES['upload']['tmp_name'][$f]; //[$count]; 
       move_uploaded_file($temp_name, "$path/"."$name"); 
       $count++; // Number of successfully uploaded file 
      } 
     } 
    } 
    } 

在它只是保存文件到一個位置的時刻,所以我不能區分開來,並確定他們來自哪個領域。

任何人都可以請幫忙嗎?

+0

修改名稱字段? '上傳[一個],上傳[兩個]'等等,然後在服務器端巧妙處理這些文件。 – Darren

+0

Hi @Darren,這就是我想要做的,但是我不知道該怎麼做。 –

+0

在循環中打印表單字段,以便每個字段都有適當的索引。 –

回答

0

我已經完成了。我已經採取了上述評論,並在每個文件字段放置了一個文本輸入字段。如果您需要設置名稱,這些也可以是隱藏字段。

然後我滾動它們並保存名稱並在服務器端應用這些名稱。

感謝評論,他們幫助我看到了解決方案。

<form action="" method="post" enctype='multipart/form-data' id="form" name="form"> 

    <input type="text" name="input0" value=""><input type="file" name="upload[]" ><br><br> 
    <input type="text" name="input1" value=""><input type="file" name="upload[]" ><br><br> 
    <input type="text" name="input2" value=""><input type="file" name="upload[]" ><br><br> 

    <button id="submit-button">Upload</button> 

</form> 


<?php 

$count = 0; 
$max_file_size = 500000000000; 
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ 
    // Loop $_FILES to exeicute all files 


$x = 0; 
$input = "input"; 


while ($x <= 2){ 

$field_name = $input.$x; 

$field[$x] = $_POST[$field_name]; 

$x++; 
} 

    foreach ($_FILES['upload']['name'] as $f => $name) { 

     $path = 'documents'; //path of directory 

     if ($_FILES['upload']['error'][$f] == 4) { 
      continue; // Skip file if any error found 
     } else { 
      if ($_FILES['upload']['size'][$f] > $max_file_size) { 
       $message[] = "$name is too large!."; 
       continue; // Skip large files 
      } 
      else { 
       $temp_name = $_FILES['upload']['tmp_name'][$f]; //[$count]; 
       move_uploaded_file($temp_name, "$path/"."$field[$count]"."$name"); 
       $count++; // Number of successfully uploaded file 
      } 
     } 
    } 
    }