【问题标题】:Storing Form element ID in variable?将表单元素ID存储在变量中?
【发布时间】:2012-11-17 09:11:07
【问题描述】:

我有一个包含以下代码的 HTML 表单:

<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"
                }

            }

    }

非常感谢您对此的任何帮助:-)

【问题讨论】:

标签: php file file-upload forms


【解决方案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
                )

        )

)

【讨论】:

  • 嗨,阿尔瓦罗!布里尔。你能告诉我(使用上面的代码)我如何使用 name 属性来做到这一点吗?
  • 感谢您的更新。我是否必须更改我的 PHP 以将数字“22”存储在变量中?
  • 是的,你必须改变你的 PHP。
  • 我很乐意回答您的任何确切问题,但我不会为您编写完整的代码。我的印象是你并不真正理解你当前的代码,这不好。
  • 感谢您迄今为止的帮助,Alvaro。感谢您的宝贵时间。
【解决方案2】:

只有name 和输入值被提交到服务器端。您可以将 ID 编码到名称中,而不是使用文件数组。

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

PHP端访问:

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 作为单独的变量发送,例如使用隐藏输入或查询字符串。

【讨论】:

  • 嗨+MrCode。你能解释一下我如何更新我的其余代码吗?例如,如何更改:$_FILES['images']['name'][$i];?
  • 谢谢。我现在就试试这个。
  • 嗨 Mr.Code - 我又来了 :) 这部分代码似乎失败了if (move_uploaded_file($file['tmp_name'], $upload_directory . $file['name'])) {,因为即使我有要上传的图像,我的脚本也会绕过它。
  • @michaelmcgurk 您的$upload_directory 是否有斜杠/?它需要它。
  • 您在 HTML 中的文件输入仍然是数组,将它们更新为我显示的 name="image_8"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多