2012-11-29 37 views
1

我有一個HTML形式包含以下代碼:將表單元素ID存儲在變量中?

<input id="image_22" name="images[]" type="file" /> 
<input id="image_8" name="images[]" type="file" /> 

我想能夠商店的形式輸入的ID元件在一個變量(例如,$ imgnumber =「image_22」) 。這可能嗎?見我在下面的代碼註釋:

PHP:

for ($i = 0; $i < count($_FILES['images']['name']); $i++) { 
     $number_of_file_fields++; 
     if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not 
      $number_of_uploaded_files++; 
      $uploaded_files[] = $_FILES['images']['name'][$i]; 

      if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) { 

       $number_of_moved_files++; 
       // *** COMMENT *** // 
       $imgnumber = CURRENT_FILE_INPUT_ID - this would store e.g. "image_22" 
       } 

      } 

    } 

非常感謝這個:-)任何幫助

+0

你可以做'圖像[22]' – Prisoner

+0

嗨,囚徒。謝謝你。你能解釋我如何修改我的代碼以允許這個嗎? – michaelmcgurk

+0

這可能也有幫助:http://stackoverflow.com/questions/5163197/how-to-get-element-id-into-php-variable – jtheman

回答

2

只有name和輸入值被提交給服務器端。您可以將ID編碼到名稱中而不是使用一組文件。在PHP端

<input id="image_22" name="image_22" type="file" /> 
<input id="image_8" name="image_8" type="file" /> 

訪問:

foreach($_FILES as $imageName => $file) 
{ 
    // $imageName contains image_22 or image_8 
    // $file contains the full array for this particular file 
} 

編輯:

你更新的PHP代碼可能是:

foreach($_FILES as $imageName => $file) 
{ 
    $number_of_file_fields++; 
    if ($file['name'] != '') { //check if file field empty or not 
     $number_of_uploaded_files++; 
     $uploaded_files[] = $file['name']; 

     if (move_uploaded_file($file['tmp_name'], $upload_directory . $file['name'])) { 

      $number_of_moved_files++; 
      // *** COMMENT *** // 
      $imgnumber = $imageName; // image_22 or image_8 
     } 
    } 
} 

做不到這一點,你將不得不送該ID作爲單獨的變量,例如使用隱藏輸入或查詢特林。

+0

偉大的一點 - 我會給它一個旋風剛剛:-) – michaelmcgurk

+0

嗨+ MrCode。你能解釋我如何更新我的代碼的其餘部分?例如,我該如何改變它:'$ _FILES ['images'] ['name'] [$ i];'? – michaelmcgurk

+0

@michaelmcgurk使用更新的代碼編輯了答案。 – MrCode

1

你不能做到這一點在純HTML + PHP的簡單的原因是,在提交表單時,瀏覽器不會向服務器發送ID。

最明顯的解決方案是在其他地方添加相關信息,例如name屬性。

更新:

不是:

<input id="image_22" name="images[]" type="file" /> 
<input id="image_8" name="images[]" type="file" /> 

...這樣做:

<input name="images[22]" type="file" /> 
<input name="images[8]" type="file" /> 

更新:

如果print_r()你的輸入,你會看到它的樣子:

Array 
(
    [images] => Array 
     (
      [name] => Array 
       (
        [22] => Test.txt 
        [8] => Blah.exe 
       ) 

      [type] => Array 
       (
        [22] => text/plain 
        [8] => application/octet-stream 
       ) 

      [tmp_name] => Array 
       (
        [22] => D:\Server\PHP\tmp\php7D2D.tmp 
        [8] => D:\Server\PHP\tmp\php7D4E.tmp 
       ) 

      [error] => Array 
       (
        [22] => 0 
        [8] => 0 
       ) 

      [size] => Array 
       (
        [22] => 3695 
        [8] => 432 
       ) 

     ) 

) 
+0

嗨,Alvaro!布里爾。你可以告訴我(使用上面的代碼)我如何使用name屬性來做到這一點? – michaelmcgurk

+0

感謝您的更新。我必須改變我的PHP,現在將數字'22'存儲在變量中嗎? – michaelmcgurk

+1

是的,你必須改變你的PHP。 –

相關問題