4

I've simplified my code for uploading a file without iFrame or flash engine, and i came up to this ajax function:

<input type="file" name="uploadfile" id="myfile" /><label for="file" id="progress"></label>

    <script src="js/jquery-1.7.1.min.js"></script>
    <script>

    function uploadFile(files) {

        var xmlhttp;
        if(window.XMLHttpRequest)
            xmlhttp = new XMLHttpRequest();
        else
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

        xmlhttp.upload.onprogress = function(e) {
            $("#progress").empty().append(e.loaded + " - " + e.total);
        }

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                alert(xmlhttp.responseText + "DONE!");
            }
        }

        xmlhttp.open("post", "post.php", true);
        xmlhttp.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
        xmlhttp.setRequestHeader("Cache-Control", "no-cache");
        xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        xmlhttp.setRequestHeader("X-File-Name", files[0].fileName);
        xmlhttp.setRequestHeader("Content-Type", "multipart/form-data");
        xmlhttp.send(files[0]);
    }



    $(document).ready(function() {

        $("#myfile").change(function() {
            uploadFile(this.files);
        });

    });

    </script>

This is the php code which reply to the ajax function:

<?php
if(isset(
        $_SERVER['CONTENT_TYPE'],
        $_SERVER['CONTENT_LENGTH'],
        $_SERVER['HTTP_X_FILE_NAME']
    ) &&
    $_SERVER['CONTENT_TYPE'] == 'multipart/form-data'){

        $file->name = basename($_SERVER['HTTP_X_FILE_NAME']);
        $input = fopen('php://input', 'rb');
    $file = fopen('files/'.$file->name, 'wb');
    stream_copy_to_stream($input, $file);
    fclose($input);
    fclose($file);
    } else {

    echo "Error";
    }
?>

The problem is, sometimes it works sometimes it bugs up while trying to upload the same file. I hope there is a solution to fix this issue. The code is simple, when i choose a file with input file type, the uploadFile function executes. When it bugs out, i can see the file starting to be uploaded but it doesnt have the original size, so somewhere it could bug and stop uploading.

Thank you in advance, Daniel!

4
  • It's just that it's broken in most browsers in the sense that it does not really solidly works. You should contact your browser vendor and discuss how to fix your issue. Commented Apr 2, 2012 at 11:35
  • well the problem is that it works but only sometimes, on small files it always works.. but on files more than 500 kb~ it works only sometimes, sometimes it stops for some reason and upload only a piece of the file Commented Apr 2, 2012 at 11:55
  • Are you getting an error when it doesn't work? Commented Apr 2, 2012 at 19:25
  • no, atleast not that Error i wrote on echo, it simply stops sometimes and i can see part of the file uploaded on server Commented Apr 3, 2012 at 7:41

1 Answer 1

1

I'm not sure it is your problem, but you should anyway make sure your server allows for uploading large enough files and can handle them without timing out.

You can set this in code or php.ini (example in code:)

ini_set('memory_limit', '96M');
ini_set('post_max_size', '64M');
ini_set('upload_max_filesize', '64M');

Then, make sure your server does not time out:

$seconds=120;
set_time_limit ( $seconds );

All this code coes on the top of your PHP file.

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

1 Comment

yes, i think the time out is the problem else i dont see any other explanation why for same file sometimes it uploads, sometimes no. ty very much for replying, Daniel.

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.