elize elize - 2 years ago 98
Javascript Question

how can I read FormData file in php?

I have an ajax formdata

<form id="form" action="index.php?id=upload" method="post" enctype="multipart/form-data">

<input id="files" multiple="true" name="files[]" type="file" />


I want send this form via dataform sequently.

So I create a loop jn jquery to read each file, So per file I have this:

var data = new FormData();
data.append(file.name, file);

url: 'index.php?id=upload',
type: 'POST',
contentType: false,
cache: false,
data: data,

in php code when I print
I get this result:

names:"array(1) { ["8_modem_pool_with_small_and_big_jpg"]=>
array(5) { ["name"]=> string(35) "8 modem pool with small
and big.jpg" ["type"]=> string(10) "image/jpeg"
["tmp_name"]=> string(24) "F:\xampp\tmp\php268B.tmp\"

["error"]=> int(0) ["size"]=> int(99790)

how can I get
value in server side?
I try




but none of them not works.


thanks for answers. but them are not my answer.

in php when I use


Iget this error:

Undefined index

But I can print values by this code:

foreach($_FILES as $index => $file) {

I hope you underestand me....

I want something like this:

//do action for single file
// do action for array file

lastest code works for normal form,But not work for formdata.

Answer Source

You can try loop through this 3 level associate array with a for loop:

    for($i=0;$i < count($_FILES['8_modem_pool_with_small_and_big_jpg']['name']);$i++){
        //Do whatever with the file:
        echo $_FILES['8_modem_pool_with_small_and_big_jpg']['name'][$i];
        echo $_FILES['8_modem_pool_with_small_and_big_jpg']['type'][$i];
        echo $_FILES['8_modem_pool_with_small_and_big_jpg']['tmp_name'][$i];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download