开发者

PHP $_FILES multiple file uploading problem

I have a little problem with uploading multiple files in PHP ,

i have this html form:

<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 :

<?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 :

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.

It's 8MB by default. If you try to upload one 5MB file everything will work. If you try to upload 2 5MB files, it exceeeds 8MB and PHP ignores posted data.

Try increasing the value of post_max_size in your php.ini.


A lot of suggestions here. I'll give it a go. This is based on @Pekka 's comment.

I see you're testing with mp3s, which probably exceed PHP upload limit. 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.. So even your fist upload didn't work. A successful upload always has 0 as the error code.

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.

I suggest testing with smaller files, as I see you have a limit of 2M for uploading.

Further reading.


Check your max_file_uploads setting -- is it more than 1?

echo ini_get('max_file_uploads');


The php.ini file must have something like this:

;;;;;;;;;;;;;;;;
; 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


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. 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.

Put the <form> element outside the element to get it to work."

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


The Problem is your name="myfile[]" attribute on your input-Element.

You cant referer later in PHP to your file, if you haven't an identifier for it. The PHP-Documentation gives you the same hint: 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜