【问题标题】:$_FILES['file'] turn null while using filepond$_FILES['file'] 在使用文件池时变为空
【发布时间】:2021-09-28 10:59:39
【问题描述】:

这是普通的 HTML 格式的输入文件,带有 POST 方法

<input type="file" name="file" accept="image/png, image/jpeg">

然后我会用这个用 PHP 编写的函数获取文件输入

function Upload() {
    $namefile = $_FILES['file']['name'];
    $error = $_FILES['file']['error'];
    $tmpName = $_FILES['file']['tmp_name'];
    move_uploaded_file($tmpName, 'upload/'.$namefile);
    return $namafile;
}

是的,这就是成功。文件移动到文件夹“上传”。

但我想尝试使用 Filepond 设置表单输入的样式。

<input class="filepond" type="file" name="file" accept="image/png, image/jpeg">

有了这个js

const inputElement = document.querySelector(".filepond");
FilePond.registerPlugin(
    FilePondPluginFileValidateType,
    FilePondPluginFileValidateSize
)
FilePond.create(inputElement, {
    labelIdle: 'Upload Photo<span class="filepond--label-action">Browse</span>',
    maxFiles: '1',
    maxFileSize: '5MB',
    labelMaxFileSizeExceeded: 'Too big bro.',
    labelMaxFileSize: 'max {filesize}',
    labelFileTypeNotAllowed: 'Meh dont up that type',
    fileValidateTypeLabelExpectedTypes: 'just {allTypes}'
})

但是当我尝试使用与以前相同的方法获取文件时,$_FILES['file'] 将变为空。

【问题讨论】:

  • 如果我没记错的话,如果你通过文件池实现上传,它会类似于$files = $_FILES["filepond"];(但如果不是这种情况,请再次查看文档)

标签: javascript php html image filepond


【解决方案1】:

filepond 可能会创建一个具有不同名称的代理元素,而不是为您的输入设置样式。

试试var_dump($_FILES),看看它是否被命名为别的名字。

【讨论】:

    【解决方案2】:

    作为browsers cannot update the file input field value,文件存储在单独的字段中。

    这意味着您要么必须将 encode the file 作为 base64 字符串,然后将其与表单提交一起上传(然后在服务器上解码)。

    或者您必须通过将FilePond server property 设置为服务器上处理上传的页面来异步上传文件。

    第三种选择是将storeAsFile 属性设置为true,FilePond 现在将更新文件输入字段,但这仅适用于浏览器supports the DataTransfer constructor

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 2021-04-30
      • 2019-11-25
      相关资源
      最近更新 更多