douda5227 2016-04-22 04:41
浏览 462
已采纳

如何在php中读取FormData文件?

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

</form>

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

        $.ajax({
            url: 'index.php?id=upload',
            type: 'POST',
            contentType: false,
            cache: false,
            processData:false,
            data: data,
            fileName:'files',

in php code when I print var_dumb($_FILES) 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 $_FILES value in server side? I try

if(isset($_FILES["files"]))
        { 

and

if(isset($_FILES["file"]))
        {

but none of them not works.

-------EDIT-------------

thanks for answers. but them are not my answer.

in php when I use

$_FILES["files"]

Iget this error:

Undefined index

But I can print values by this code:

foreach($_FILES as $index => $file) {
 move_uploaded_file($file['tmp_name'],$target.$file['name']); 
}

I hope you underestand me....

I want something like this:

if(isset($_FILES["files"]))
{
   //do action for single file
   // do action for array file
}

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

  • 写回答

3条回答 默认 最新

  • dongyi2006 2016-04-22 05:43
    关注

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

    if(isset($_FILES['8_modem_pool_with_small_and_big_jpg'])){
        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];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 umi接入sentry遇到问题
  • ¥15 HBuilderX打包H5网页,扫码模块无法使用
  • ¥15 Javascript跳转页面后,无法执行后面代码,如何解决?
  • ¥15 echarts绘制图表
  • ¥15 请教两个关于高德地图定位不准的技术问题
  • ¥15 根据企业名称 对照两个文件 样本筛选/匹配
  • ¥15 Linux环境下CA证书更新问题
  • ¥15 sqlserver语句提取结果以外数据
  • ¥60 微信小程序如何上传QQ聊天文件
  • ¥300 开发的系统遭到无良商家的破解,请问如何防止再次发生,并追回损失