【问题标题】:unable to uploading large file using php and pure ajax无法使用 php 和纯 ajax 上传大文件
【发布时间】:2018-12-23 00:39:25
【问题描述】:

我正在尝试在我的 php 服务器上上传大约 200MB 的大文件,我已经配置了我的 php.ini 文件:-

 upload_max_filesize = 9000M

我使用过纯 ajax :-

function _(el){
   return document.getElementById(el);
}
function uploadFile(){
   var file = _("file1").files[0];
   var formdata = new FormData();
   formdata.append("file1", file);
   var ajax = new XMLHttpRequest();
   ajax.addEventListener("load", completeHandler, false);
   ajax.addEventListener("error", errorHandler, false);
   ajax.addEventListener("abort", abortHandler, false)
   ajax.open("POST", "index.php");
   ajax.send(formdata);
 }
 function completeHandler(event){
     _("status").innerHTML = event.target.responseText;
     _("progressBar").value = 0;
}
function errorHandler(event){
    _("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
   _("status").innerHTML = "Upload Aborted";
} 

我的 PHP 代码:-

<?php
  $fileName = $_FILES["file1"]["name"]; e
  $fileTmpLoc = $_FILES["file1"]["tmp_name"]; 
  $fileType = $_FILES["file1"]["type"]; 
  $fileSize = $_FILES["file1"]["size"]; 
  $fileErrorMsg = $_FILES["file1"]["error"]; 
  if (!$fileTmpLoc) { 
    echo "ERROR: Please browse for a file before clicking the upload button.";
   exit();
 }
 if(move_uploaded_file($fileTmpLoc, "uploads/$fileName")){
    echo "$fileName upload is complete";
 } else {
    echo "move_uploaded_file function failed";
 }
?>

问题:- 当我上传小于或等于 7MB 的文件时,我的文件将成功上传,但当我尝试上传大文件时,我会收到错误

错误:-

 Notice: Undefined index: file1 in C:\wamp\www\test\index.php on line 2
 Notice: Undefined index: file1 in C:\wamp\www\test\index.php on line 3
 Notice: Undefined index: file1 in C:\wamp\www\test\index.php on line 4
 Notice: Undefined index: file1 in C:\wamp\www\test\index.php on line 5
 Notice: Undefined index: file1 in C:\wamp\www\test\index.php on line 6

ERROR: Please browse for a file before clicking the upload button.
Uploaded 34209942 bytes of 34209942

为什么我会遇到这个问题? 我怎么解决这个问题 ? 如何使用 ajax 上传最大 200MB 的文件?

请不要向我推荐使用 JQUERY 或任何其他外部库或框架的想法

【问题讨论】:

    标签: php ajax file-upload


    【解决方案1】:

    尝试在 php.ini 中编辑 post_max_size 指令。可能是您的服务器不允许超过 7Mb 的 POST 请求,因此 $_FILES["file1"] 变量不存在。

    【讨论】:

      猜你喜欢
      • 2019-08-28
      • 2018-11-20
      • 2020-03-14
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 2014-05-02
      • 2014-09-07
      相关资源
      最近更新 更多