简体   繁体   中英

ajax php file upload without iFrame or flash engine

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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