繁体   English   中英

从单个文件输入类型一次转换为多个文件上传。 纽伯的东西

[英]Converting to multiple file upload at once from single file input types. Newb Stuff

我有一个脚本,可以通过表单上传图像。 我想从:

<input type="file" name="img_1_upload" class="text">
<input type="file" name="img_2_upload" class="text">
<input type="file" name="img_3_upload" class="text">
<input type="file" name="img_4_upload" class="text">
<input type="file" name="img_5_upload" class="text">
<input type="file" name="img_6_upload" class="text">

通过以下方式使用多重上传输入类型:

<input name="images[]" id="filesToUpload" type="file" multiple="" />

我的PHP脚本检测到输入名称为img_1_upload,img_2_upload,img_3_upload ...的图像,一直到img_6_upload ...

但是,使用多文件输入类型时,我无法将每个文件的名称设置为img_1_upload等格式。

处理此问题的最佳方法是什么?

您是否尝试过move_uploaded_file()函数? 无论如何,上传的文件都是临时文件。 您可能需要将它们移动到更合适的位置。 移动时,还可以将文件重命名为所需的文件。

PS:我假设您知道可以使用$_FILES["images"]["name"][index]访问文件名,其中index是从0到文件数-1的数组索引。

P.S2:还请确保您的窗体具有多个文件的enctype="multipart/form-data"属性。

$ _FILES数组包含上载文件的信息。 上传多个文件时,$ _ FILES的结构类似; 数组

(
    [name] => Array
        (
            [0] => foo.txt
            [1] => bar.txt
        )

    [type] => Array
        (
            [0] => text/plain
            [1] => text/plain
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpYzdqkD
            [1] => /tmp/phpeEwEWG
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 123
            [1] => 456
        )
)

然后,您可以使用简单的foreach()循环随意重命名文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM