简体   繁体   English

使用PHP和Ajax上传文件

[英]File upload using PHP and Ajax

I am able to upload small files without any issues. 我可以上传小文件,没有任何问题。 When I try with a 150mb file, it stops unexpectedly. 当我尝试使用150mb文件时,它会意外停止。 The following is my HTML file: 以下是我的HTML文件:

<html>
  <body>
    <form  method="post" enctype="multipart/form-data" class="form-horizontal" id="form_upload">
      <div class="control-group">
        <label class="control-label muted">IMG</label>
        <div class="controls">
          <input type="file" id="img" name="img" />
        </div>
      </div>
      <div class="control-group">
        <label class="control-label muted">MD5</label>
        <div class="controls">
          <input type="file" id="md5" name="md5" />
        </div>
      </div>
    </form>
    <div class="progress">
      <div class="bar"></div>
      <div class="percent"></div>
    </div>
  </body>
</html>

And the following is my Ajax handler: I am using the jQuery form submit plugin. 以下是我的Ajax处理程序:我正在使用jQuery表单提交插件。

uploadFirmware:function(){
  var bar = $('.bar');
  var percent = $('.percent');
  self = this;
  var options = {
    beforeSend: function() {
      var percentVal = '0%';
      bar.width(percentVal)
      percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
      var percentVal = percentComplete + '%';
      bar.width(percentVal)
      percent.html(percentVal);
    },
    complete: function(data) {
      console.log(data);
      self.showStatusMsg(1,"<b>Firmware Upload: </b> File Upload successfully");
    },
    url: '/cav_firmware/api',
    iframe: false
  };
  $("#form_upload").ajaxSubmit(options);
  return false;
}

And finally this is my PHP code. 最后这是我的PHP代码。 I am using CodeIgniter, but I am not using that in my code, just a classic PHP script. 我正在使用CodeIgniter,但我没有在我的代码中使用它,只是一个经典的PHP脚本。

public function api_post(){
  $filea = $_FILES['img'];
  $fileb = $_FILES['md5'];
  if( move_uploaded_file($filea['tmp_name'], '/home/sreeni/upload/'.$filea['name']) && move_uploaded_file($fileb['tmp_name'], '/home/sreeni/upload/'.$fileb['name'])){
    echo "Success fully upload files";
  } else {
    echo "File upload failed.";
  }
}

All this is fine. 这一切都很好。 My issue is only with uploading a 150 MB file. 我的问题只是上传一个150 MB的文件。 I am getting the following error when I try to upload my MD5 and IMG files: 我尝试上传MD5和IMG文件时收到以下错误:

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
  <h4>A PHP Error was encountered</h4>
  <p>Severity: Notice</p>
  <p>Message:  Undefined index: img</p>
  <p>Filename: controllers/cav_firmware.php</p>
  <p>Line Number: 30</p>
</div>
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
  <h4>A PHP Error was encountered</h4>
  <p>Severity: Notice</p>
  <p>Message:  Undefined index: md5</p>
  <p>Filename: controllers/cav_firmware.php</p>
  <p>Line Number: 31</p>
</div>File upload failed.

I changed my php.ini to the maximum size 200 MB (memory limit) in /etc/php5/apache2/php.ini . 我改变了我php.ini中的最大大小200 MB(内存限制) /etc/php5/apache2/php.ini

What am I doing wrong here? 我在这做错了什么? Do I need to specify my files are MD5 and IMG (firmware image files)? 我是否需要指定我的文件是MD5和IMG(固件映像文件)? Any help? 有帮助吗?

在php.ini中检查脚本的最大执行时间默认时间是30秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM