concrete5 - how to get files uploaded through ajax in php

后端 未结 1 805
醉梦人生
醉梦人生 2021-01-29 11:41

I\'m trying to upload multiple files through ajax but I can\'t figure out how to get the uploaded files in PHP. I sent them

var attachments = $(\'.attachment-file         


        
相关标签:
1条回答
  • 2021-01-29 12:00

    Working solution:

    Here's how you send multiple files with ajax.

    JS:

    var post_data = new FormData();
    attachments.each(function(i, v) {
        post_data.append('my_file[]', v.files[0]);
        console.log(v.files[0]);
    });
    

    PHP:

    $files = $this->request->files;
    $files = $files->get('my_file');
    foreach ($files as $file) {
        \Log::Info(var_dump_safe($file->getClientOriginalName()));
    }
    

    It's all working now.

    PS. Don't forget not to post and upload files bigger than the PHP limits of post_max_size and upload_max_size!

    0 讨论(0)
提交回复
热议问题