简体   繁体   English

PHP $ _FILES多文件上传问题

[英]PHP $_FILES multiple file uploading problem

I have a little problem with uploading multiple files in PHP , 我在PHP中上传多个文件时遇到一些问题,

i have this html form: 我有这个HTML格式:

<form method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="myfile[]"  />
    <input type="submit" />
</form>

and this is the upload.php : 这是upload.php:

<?php print_r( $_FILES ); ?> 

when i'm sending a file it show me this: 当我发送文件时,它告诉我这个:

 Array
(
[myfile] => Array
    (
        [name] => Array
            (
                [0] => Krw_Qe4QKmI.mp3
            )

        [type] => Array
            (
                [0] => 
            )

        [tmp_name] => Array
            (
                [0] => 
            )

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

        [size] => Array
            (
                [0] => 0
            )

    )

 )

so far so good. 到现在为止还挺好。

the problem starts when i upgrade my form to this one : 当我将表单升级到此表单时问题开始:

<form method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="myfile[]"  />
    <input type="file" name="myfile[]"  />
    <input type="submit" />
</form>

now , when i send 2 files , it show me this : 现在,当我发送2个文件时,它显示了这个:

Array
(
)

so , what's the problem here? 那么,这里的问题是什么? thank you , Mor. 谢谢你,莫尔。

I would bet that you exceeded post_max_size and PHP just ignored the uploaded files. 我敢打赌你超过了post_max_size而PHP只是忽略了上传的文件。

It's 8MB by default. 它默认为8MB。 If you try to upload one 5MB file everything will work. 如果您尝试上传一个5MB文件,一切都会正常工作。 If you try to upload 2 5MB files, it exceeeds 8MB and PHP ignores posted data. 如果您尝试上传2个5MB文件,则超过8MB,PHP会忽略发布的数据。

Try increasing the value of post_max_size in your php.ini. 尝试在php.ini中增加post_max_size的值。

A lot of suggestions here. 这里有很多建议。 I'll give it a go. 我会试一试。 This is based on @Pekka 's comment. 这是基于@Pekka的评论。

I see you're testing with mp3s, which probably exceed PHP upload limit. 我看到你正在测试mp3,这可能超过PHP上传限制。 This is because in your first example, you actually have an upload error code 1: The uploaded file exceeds the upload_max_filesize directive in php.ini. 这是因为在您的第一个示例中,您实际上有一个上载错误代码1: The uploaded file exceeds the upload_max_filesize directive in php.ini. . So even your fist upload didn't work. 所以即使你的拳头上传也无效。 A successful upload always has 0 as the error code. 成功上传的错误代码始终为0。

Modify you php.ini with upload_max_filesize = 10M (or 20M , or 300M ; careful about that M - which means megabytes - as omitted, brings alot of headache. upload_max_filesize = 10M (或20M ,或300M修改你的php.ini;小心那个M - 这意味着兆字节 - 省略,带来很多头痛。

I suggest testing with smaller files, as I see you have a limit of 2M for uploading. 我建议使用较小的文件进行测试,因为我发现上传的限制为2M。

Further reading . 进一步阅读

检查你的max_file_uploads设置 - 它是否超过1?

echo ini_get('max_file_uploads');

The php.ini file must have something like this: php.ini文件必须是这样的:

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =

; Maximum allowed size for uploaded files.
upload_max_filesize = 50M


; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

Change the values of upload_max_filesize and then restart the server 更改upload_max_filesize的值,然后重新启动服务器

To do multiple files at once, try giving an index like this: 要一次执行多个文件,请尝试提供如下索引:

<? For ( $count = 0; count < SOME_MAXIMUM; ++$count; ): ?>
<input type="file" name="myfile[<? Echo $count; ?>]"  />
<? endfor; ?>

I had the same problem.. All my efforts were in vain, but finally I found a pretty good note at the PHP Manual. 我遇到了同样的问题..我所有的努力都是徒劳的,但最后我在PHP手册上找到了一个很好的说明。 It's simple but suited me perfectly... 这很简单但很适合我......

"Multiple upload might not work if you use a table for displaying your form inputs when <form> element is inside the <table> element. In this case only the first file will be uploaded . “当<form>元素位于 <table>元素时,如果使用表格来显示表单输入,则多次上传可能不起作用。在这种情况下, 只会上传第一个文件

Put the <form> element outside the element to get it to work." <form>元素放在元素之外以使其工作。“

Follow this link for the full note, there is a function to rearrange the multiple upload file array in a easy-to-use manner. 按照此链接获取完整备注,有一个功能可以以易于使用的方式重新排列多个上传文件阵列。

http://php.net/manual/en/features.file-upload.multiple.php http://php.net/manual/en/features.file-upload.multiple.php

The Problem is your name="myfile[]" attribute on your input-Element. 问题是你的input-Element上的name="myfile[]"属性。

You cant referer later in PHP to your file, if you haven't an identifier for it. 如果您没有标识符,则无法在以后将PHP引用到您的文件中。 The PHP-Documentation gives you the same hint: http://www.php.net/manual/en/features.file-upload.multiple.php PHP文档为您提供了相同的提示: http//www.php.net/manual/en/features.file-upload.multiple.php

Multiple files can be uploaded using different name for input. 可以使用不同的名称上传多个文件以进行输入。

So change the name to "myfile1" and "myfile2" (or some better name ;)) should solve your problem. 所以将名称更改为“myfile1”和“myfile2”(或更好的名称;))应该可以解决您的问题。

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

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