简体   繁体   English

上传文件大小问题(HTML5,Javascript,PHP)

[英]File size issue with upload (HTML5, Javascript, PHP)

I followed a tutorial on the internet how to create an upload form with HTML5, Javascript and PHP. 我遵循了互联网上的教程,该教程如何使用HTML5,Javascript和PHP创建上传表单。 When I try to upload, I upload small files just fine, but if I enter something bigger it gives me an error. 尝试上传时,我可以上传较小的文件,但是如果输入较大的文件,则会出现错误。 I figured that my first error was because of IIS not being configured to upload big files so I fixed that, from then on whenever I try to get the file from PHP I don't get anything. 我发现我的第一个错误是因为未将IIS配置为上传大文件,所以我解决了这个问题,从那时起,每当我尝试从PHP获取文件时,我一无所获。 I get empty parameters. 我得到空参数。 Here are some pieces of my code: 这是我的一些代码:

HTML: HTML:

<form id="upload_form" enctype="multipart/form-data" method="post">
  <input type="file" name="file1" id="file1"><br>
  <input type="button" value="Upload File" onclick="uploadFile()">
  <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
  <h3 id="status"></h3>
  <p id="loaded_n_total"></p>
</form>

JavaScript: JavaScript:

function uploadFile(){
  var file = _("file1").files[0];
  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", "file_upload_parser.php");
  ajax.send(formdata);
}

PHP: PHP:

<?php
$fileName = $_FILES["file1"]["name"]; 
$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, "savefiles/$fileName")){
    echo "$fileName upload is complete";
} else {
    echo "move_uploaded_file function failed";
}
?>

What is the best way to handle the upload of big files? 处理大文件上传的最佳方法是什么? I am looking for a way to upload files around 2GBs big. 我正在寻找一种上传大小约为2GB的文件的方法。

To upload big files, instead of upsizing the upload limit in the server you need to use chunk upload with a library like plupload which allow request chunking. 要上传大文件,而不是在服务器中增加上传限制,您需要使用带有类似plupload这样的库的块上传,该库允许请求分块。

Request chunking will allow the server to ask the client browser to send the large file as small chunks. 请求分块将允许服务器请求客户端浏览器将大文件作为小块发送。 This is efficient, and server load is reduced in case of large files. 这是有效的,并且在大文件的情况下减少了服务器负载。 Also, it also allows for file upload resuming which is very much required in case of large file uploads. 此外,它还允许恢复文件上传 ,这在大文件上传的情况下非常需要。

Instead if you go increasing your post_max_size and upload_max_filesize, prepare for a server melt down! 相反,如果您要增加post_max_size和upload_max_filesize,请准备使服务器崩溃!

You need to increase your post_max_size (defaults to 8M) and upload_max_filesize (defaults to 2M) in your php.ini file. 您需要在php.ini文件中增加post_max_size (默认为8M)和upload_max_filesize (默认为2M)。

You'll also need to change memory_limit as well so that it is >= post_max_size as well as max_execution_time 您还需要更改memory_limit ,使其> = post_max_size以及max_execution_time

memory_limit and max_execution_time can be set in the script itself using ini_set 可以使用ini_set在脚本本身中设置memory_limitmax_execution_time

Note: this is probably not the best answer if this isn't just home project that only you will be using (and if it why not just set up an ftp server instead of use php). 注意:如果这不仅仅是您将要使用的家庭项目,那么这可能不是最佳答案(如果不这样做,为什么不设置ftp服务器而不使用php)。 Though, if you have multiple users I would go with the answer provided by @SrijithVijayamohan 但是,如果您有多个用户,我会使用@SrijithVijayamohan提供的答案

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

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