0

I followed a tutorial on the internet how to create an upload form with HTML5, Javascript and 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. I get empty parameters. Here are some pieces of my code:

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:

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
$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.

2 Answers 2

3

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.

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Just so you know it wasn't I that voted your answer. I never said my answer was the best one but if this is just a simple home project for the OP than going through the extra work using plupload might not be what the OP wants to do and if only he/she will be using it than there is nothing wrong with simply changing the upload size limits. Your way may be the proper way but now the OP has a couple options and can choose the best one for the given situation
3

You need to increase your post_max_size (defaults to 8M) and upload_max_filesize (defaults to 2M) in your php.ini file.

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 and max_execution_time can be set in the script itself using ini_set

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). Though, if you have multiple users I would go with the answer provided by @SrijithVijayamohan

2 Comments

The question specifies for a large file size upto 2GB. Upsizing the server upload size is the wrong way to do it.
Both of you have valid points. I chose the answer of Srijith, because of it offered efficiency. It's a work project and I was looking for efficiency too, not just to get the job done. It's a great answer too. It helped with one of the other uploaders I tried. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.