如何将多个文件从 Vue js 发送到 PHP

Posted

技术标签:

【中文标题】如何将多个文件从 Vue js 发送到 PHP【英文标题】:how to send multiple files from Vue js to PHP 【发布时间】:2021-06-27 06:51:39 【问题描述】:
<form method="POST" @submit.prevent="share" >
            <div id="imgs">
            
            Select Images: <input type="file" ref="file" name="file[]" multiple ></form>

      share()
        var formData=new FormData();
         
  for( var i = 0; i < this.$refs.file.files.length; i++ )
        let file = this.$refs.file.files[i];
        formData.append('file[' + i + ']', file);
    ;

    
        axios.post('http://localhost/11/upload_img.php',formData,
            headers:'Content-Type':'multipart/form-data'
        ).then((res)=>
            console.log("sent");
            
        );
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script>
<form method="POST" @submit.prevent="share" >
                <div id="imgs">
                
                Select Images: <input type="file" ref="file" name="file[]" multiple >
        
        
        
  

我像上面一样在 vue js 中发送文件 这是我的 PHP 代码$filename=$_FILES['file']['name'];

$files=$_FILES['file'];

回声($files);

我得到了这个错误

注意:未定义索引:第 6 行 C:\xampp\htdocs\11\upload_img.php 中的文件

【问题讨论】:

【参考方案1】:

您似乎没有为您的表单 enctype 属性设置“multipart/form-data”,所以您不会在后端获得 $_FILES['file']

检查What does enctype='multipart/form-data' mean?

【讨论】:

现在我做了,但还是同样的错误,我需要改变什么吗? var_dump($_FILES) 的结果是什么?

以上是关于如何将多个文件从 Vue js 发送到 PHP的主要内容,如果未能解决你的问题,请参考以下文章