douhuireng4407 2018-07-07 17:35
浏览 168

我的文件上传正在崩溃我的服务器

So I am trying to learn PHP and JS and wanted to build a file uploader for my home router running OpenWRT. I followed a lot of guides and built one that worked satisfactorily. However, when I started using bigger files (> 5mb) the router itself began to crash. When uploading an 11MB files, the router could crash anywhere between 30%-75% uploaded. I have edited php.ini like suggested by other posts on this site to support larger files and longer php script run times. I also have began to research chunking for file upload, but wanted to confirm where my problem was first. I apologize if my code is sloppy, I am still trying to figure everything out.

function _(el) {
  return document.getElementById(el);
}

function uploadFile() {
  document.getElementById('progressBar').style.display = "block";
  var file = _("file1").files[0];
  if(_("file1").files[0] == null){
    _("status").innerHTML = "You Must Select a File";
  }
  else{
      // alert(file.name+" | "+file.size+" | "+file.type);
    var formdata = new FormData();
    formdata.append("file1", file);
    var ajax = new XMLHttpRequest();

    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.addEventListener("error", errorHandler, false);
    ajax.addEventListener("abort", abortHandler, false);
    ajax.open("POST", "UPLOADER.php");  
    ajax.send(formdata);
  }
  
}
    <form id="upload_form" enctype="multipart/form-data" method="post">
       <input type="file" name="file1" id="file1" class="inputfile" data-multiple-caption="{count} files selected" multiple />
            <button onclick="uploadFile()" class="buttonUpload">Upload</button>
   </form>    

And my PHP is

<?php
ini_set('upload_max_filesize', '40M');
ini_set('post_max_size', '40M');
ini_set('max_input_time', 300);
ini_set('max_execution_time', 300);
$fileName = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}
if(move_uploaded_file($fileTmpLoc, "Home/$fileName")){
    echo "$fileName upload is complete";
} else {
    echo "move_uploaded_file function failed";
}
?>

So it looks like my problem is that I need to perform the upload using chunking. I have seen all of the plugins such as resumable.js and plupload, but I would really like to understand how to do this within the context of the code I already have. Is that possible?

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 手机连接电脑热点显示无ip分配
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大