0

I'm not trying to do any unusual stuff, all I need is a simple ajax photo upload with HTML5 progress bar, so my code is so ordinary:

<!DOCTYPE html>
<html>
<head>
    <script>
        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.upload.addEventListener("progress", progressHandler, false);
            ajax.addEventListener("load", complateHandler, false);
            ajax.addEventListener("error", errorHandler, false);
            ajax.addEventListener("abort", abortHandler, false);
            ajax.send(formdata);
        }
        function progressHandler(event){
            _("loaded_n_total").innerHTML = "Uploaded" +event.loaded+" bytes of "+event.total;
            var percent = (event.loaded / event.total) * 100;
            _("progressBar").value = Math.round(percent);
            _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
        }
        function complateHandler(event){
            _("status").innerHTML = event.target.responseText;  
            _("progressBar").value = 0;
        }
        function errorHandler(event){
            _("status").innerHTML = "Upload Failed";    
        }
        function abortHandler(event){
            _("status").innerHTML = "Upload Aborted";
        }
    </script>
</head>
<body>

    <h2>Upload Photos</h2>

    <form id="upload_form" enctype="multipart/form-data" method="POST">
        <input type="file" id="file1" multiple /><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>


</body>
</html>

up.php:

<?php
set_time_limit(100000);
$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, "uploads/$fileName")){
echo "$fileName upload is complate";    
}else{
echo "move_uploaded_file function failed";
}
?>

Everything go so well, except when trying to upload photos more than 300 K.B, the progress bar reach 70% then start over reach 65% or something start over again and finally failed !!!

I tried to add the following to the .htaccess file: php_value upload_max_filesize 20M php_value post_max_size 20M php_value max_execution_time 200 php_value max_input_time 200

but nothing changed.

Tarek.hms
  • 1,243
  • 1
  • 10
  • 15

1 Answers1

0

You should use @getimagesize() function to validate the image. If it's not an image (not just the extension) it will return false.

if(list($width, $height, $type, $attributes) = @getimagesize($_FILES['userfile']['tmp_name'])) 

Maybe your error comes from the memory limit value. It use to be 128M. Try changing to 256M to ensure that it's not the issue. Anyway, if it is, you should change your code to make it lighter.

Manolo
  • 24,020
  • 20
  • 85
  • 130