2017-01-30 385 views
-1

檢查圖像後我有3個輸入:如何通過ID在PHP

應用程序名稱

2.背景照片

標誌相片

我從@mith那裏獲得幫助。代碼很棒。圖像總是進入正確的文件夾。將代碼提交到文件夾後,我調整代碼以更改圖像的名稱。但我不知道這種情況有什麼問題

$fieldname = ($key == 'image[]') ? 'image' : 'logo'; 
$filename = $applicationName . '_' . $fieldname . '.' . 
      pathinfo($upload["tmp_name"], PATHINFO_EXTENSION); 

$filename永遠是標誌。我不知道爲什麼這種情況總是假的。 因此,2張圖片總是被命名爲applicationName_logo。請幫我弄清楚。

HTML表單:

<form action="yong.php" method="POST" enctype="multipart/form-data"> 
 
    <h3>App name</h3> 
 
    <input type="text" id="applicationName" name="applicationName"> 
 
    <h3>Background image</h3> 
 
    <input type="file" id="image" name="image[]" multiple="multiple" accept="image/*" /> 
 
    <h3>Logo image</h3> 
 
    <input type="file" id="logo" name="logo[]" multiple="multiple" accept="image/*" /> 
 
    <br> 
 
    <br> 
 
    <input type="submit"> 
 
</form>

PHP代碼:

<?php 
$valid_formats = array("jpg", "png", "gif", "zip", "bmp"); 
$max_file_size = 5000*100; //100 kb 
$path = "home_dir/"; // Upload directory 
$count = 0; 

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

$applicationName = $_POST['applicationName']; 
$sql_field_list = ['applicationName']; 
$sql_value_list = [$applicationName]; 

foreach ($_FILES['image']['name'] as $f => $name) { 
    if ($_FILES['image']['error'][$f] == 4) { 
     continue; // Skip file if any error found 
     echo "Skip file if any error found"; 
    } 
    if ($_FILES['image']['error'][$f] == 0) { 
     if ($_FILES['image']['size'][$f] > $max_file_size) { 
      $message[] = "$name is too large!."; 
      echo "$name is too large!."; 
      continue; // Skip large files 
     } 
     elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){ 
      $message[] = "$name is not a valid format"; 
      echo "$name is not a valid format"; 
      continue; // Skip invalid file formats 
     } 
     else{ // No error found! Move uploaded files 
      $tmp_name = $upload["tmp_name"]; 
      $parts  = explode('/', $upload['tmp_name']); 
      $tmpName = array_pop($parts); 
      $fieldname = ($key == 'image[]') ? 'image' : 'logo'; 
      $filename = $applicationName . '_' . $fieldname . '.' . pathinfo($upload["tmp_name"], PATHINFO_EXTENSION); 
     } 

      //if(move_uploaded_file($_FILES["image"]["tmp_name"][$f], $path.$filename.png)) 
      if(move_uploaded_file($_FILES["image"]["tmp_name"][$f], $path.'applicationName_bg/'.$filename.png)) 
       $count++; // Number of successfully uploaded file 
      $message[] = "$name is uploaded"; 
      echo "$filename is uploaded"; 

    } 
} 

foreach ($_FILES['logo']['name'] as $f => $name) { 
    if ($_FILES['logo']['error'][$f] == 4) { 
     continue; // Skip file if any error found 
     echo "Skip file if any error found"; 
    } 
    if ($_FILES['logo']['error'][$f] == 0) { 
     if ($_FILES['logo']['size'][$f] > $max_file_size) { 
      $message[] = "$name is too large!."; 
      echo "$name is too large!."; 
      continue; // Skip large files 
     } 
     elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){ 
      $message[] = "$name is not a valid format"; 
      echo "$name is not a valid format"; 
      continue; // Skip invalid file formats 
     } 
     else{ // No error found! Move uploaded files 
      $tmp_name = $upload["tmp_name"]; 
      $parts  = explode('/', $upload['tmp_name']); 
      $tmpName = array_pop($parts); 
      $fieldname = ($key == 'image[]') ? 'image' : 'logo'; 
      $filename = $applicationName . '_' . $fieldname . '.' . pathinfo($upload["tmp_name"], PATHINFO_EXTENSION); 
     } 

      //if(move_uploaded_file($_FILES["logo"]["tmp_name"][$f], $path.$filename.png)) 
      if(move_uploaded_file($_FILES["logo"]["tmp_name"][$f], $path.'applicationName_logo/'.$filename.png)) 
       $count++; // Number of successfully uploaded file 
      $message[] = "$name is uploaded"; 
      echo "$filename is uploaded"; 

    } 
} 
} 
+0

使用'文件[ '形象'] []'和'文件[ '標誌'] []',並自動將有所區別。你可以在你的php代碼中打印'$ _FILES'來檢查它。它會給你多維數組,這是'image'和'logo' –

+0

PHP不識別'id'它知道'name'兩個主要按鍵。 –

+0

使用鍵區分輸入字段。 PHP無法默認讀取HTML ID屬性。區分輸入文件的最好方法是用鍵分離它們。 e.g

回答

1

試試下面的代碼單獨保存的標誌和圖像文件:

HTML:

<form action="yong.php" method="POST" enctype="multipart/form-data"> 
    <h3>App name</h3> 
    <input type="text" id="applicationName" name="applicationName"> 
    <h3>Background image</h3> 
    <input type="file" id="image" name="image[]" multiple="multiple" accept="image/*" /> 
    <h3>Logo image</h3> 
    <input type="file" id="logo" name="logo[]" multiple="multiple" accept="image/*" /> 
    <br> 
    <br> 
    <input type="submit"> 
</form> 

PHP

<?php 
$valid_formats = array("jpg", "png", "gif", "zip", "bmp"); 
$max_file_size = 5000*100; //100 kb 
$path = "home_dir/"; // Upload directory 
$count = 0; 

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ 
// Loop $_FILES to exeicute all files 
foreach ($_FILES['image']['name'] as $f => $name) { 
    if ($_FILES['image']['error'][$f] == 4) { 
     continue; // Skip file if any error found 
     echo "Skip file if any error found"; 
    } 
    if ($_FILES['image']['error'][$f] == 0) { 
     if ($_FILES['image']['size'][$f] > $max_file_size) { 
      $message[] = "$name is too large!."; 
      echo "$name is too large!."; 
      continue; // Skip large files 
     } 
     elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){ 
      $message[] = "$name is not a valid format"; 
      echo "$name is not a valid format"; 
      continue; // Skip invalid file formats 
     } 
     else{ // No error found! Move uploaded files 
      if(move_uploaded_file($_FILES["image"]["tmp_name"][$f], $path.'applicationName_bg/'.$name)) 
       $count++; // Number of successfully uploaded file 
      $message[] = "$name is uploaded"; 
      echo "$name is uploaded"; 
     } 
    } 
} 

foreach ($_FILES['logo']['name'] as $f => $name) { 
    if ($_FILES['logo']['error'][$f] == 4) { 
     continue; // Skip file if any error found 
     echo "Skip file if any error found"; 
    } 
    if ($_FILES['logo']['error'][$f] == 0) { 
     if ($_FILES['logo']['size'][$f] > $max_file_size) { 
      $message[] = "$name is too large!."; 
      echo "$name is too large!."; 
      continue; // Skip large files 
     } 
     elseif(! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)){ 
      $message[] = "$name is not a valid format"; 
      echo "$name is not a valid format"; 
      continue; // Skip invalid file formats 
     } 
     else{ // No error found! Move uploaded files 
      if(move_uploaded_file($_FILES["logo"]["tmp_name"][$f], $path.'applicationName_logo/'.$name)) 
       $count++; // Number of successfully uploaded file 
      $message[] = "$name is uploaded"; 
      echo "$name is uploaded"; 
     } 
    } 
} 
} 
+1

創建文件夾applicationName_logo和applicationName_bg內部HOME_DIR並嘗試以上回答。我在我的代碼中更改了上傳路徑。 @Splicee – mith

+0

我有一些問題。 但我不能在評論框中問你,因爲它太長了。請重新讀取 @mith – Splicee