PHP& AJAX上传文件有时会失败

时间:2013-08-23 06:17:34

标签: php javascript ajax forms xmlhttprequest

我正在为成人网站创建一个简单的视频上传器,它适用于小文件但不适用于较大的文件。我没有测试断线是什么,但我知道它确实适用于5mb左右的文件,并且100mb +文件失败。在服务器中,我在php5.ini中将最大上传大小和最大帖子大小设置为1000mb。你能发现代码有什么问题吗?或者你认为这是服务器问题?该网站托管在一个linux godaddy服务器中。以下是测试链接:

http://munchmacouchi.com/upload_sample/upload.php

以下是upload.js中的代码:

    var handleUpload = function(event){
    event.preventDefault();
    event.stopPropagation();

    var fileInput = document.getElementById('file');

    var data = new FormData();
    data.append('ajax',true);

    for ( var i = 0 ; i < fileInput.files.length ; i++ ){
        data.append('file[]',fileInput.files[i]);
    }

    var request = new XMLHttpRequest();

    request.upload.addEventListener('progress', function(event){
        if(event.lengthComputable){
            var percent = event.loaded / event.total ;
            var progress = document.getElementById('upload_progress');

            while(progress.hasChildNodes())
                progress.removeChild(progress.firstChild);

            progress.appendChild(document.createTextNode(Math.round(percent*100)+' %'));
        }
    });
    request.upload.addEventListener('load', function(event){
        document.getElementById('upload_progress').style.display='none';
    });

    request.upload.addEventListener('error', function(event){
        alert('Upload Failed');
    });

    request.addEventListener('readystatechange', function(event){
        if(this.readyState == 4){
            if(this.status == 200){
                var links = document.getElementById('uploaded');
                var uploaded = eval(this.response);
                var div, a;
                for(var i = 0 ; i < uploaded.length ; i++){
                    div = document.createElement('div');
                    a = document.createElement('a');
                    a.setAttribute('href','files/'+uploaded[i]);
                    a.appendChild(document.createTextNode(uploaded[i]));
                    div.appendChild(a);
                    links.appendChild(div);
                }
            }else{
                console.log("Server replied with HTTP status " + this.status);
            }
        }
    });

    request.open('POST','upload.php');
    request.setRequestHeader('Cache-Control', 'no-cache');
    document.getElementById('upload_progress').style.display='block';
    request.send(data);
}
window.addEventListener('load',function(event){
    var submit = document.getElementById('submit');
    submit.addEventListener('click',handleUpload);
});

和upload.php:

    <?php 
if(!empty($_FILES['file'])){
    foreach($_FILES['file']['name'] as $key => $name){
        if($_FILES['file']['error'][$key] == 0 && move_uploaded_file($_FILES['file']['tmp_name'][$key], "files/{$name}")){
            $uploaded[] = $name;
        }
    }

    if(!empty($_POST['ajax'])){
        die(json_encode($uploaded));
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Upload Test</title>
        <h3>Upload Test</h3>
        <script type="text/javascript" src="upload.js"></script>
        <style type="text/css">
            #upload_progress {display:none;}
        </style>
    </head>

    <body>
        <div id="uploaded">
            <?php
            if(!empty($uploaded)){
                foreach($uploaded as $name){
                    echo '<div><a href="files/', $name, '">', $name , '</a></div>';
                }
            }
            ?>
        </div>
        <div id="upload_progress"></div>

        <div>
            <form action="" method="post" enctype="multipart/form-data">
                <div> 
                    <input type="file" id="file" name="file[]" multiple="multiple"/>
                    <input type="submit" id="submit" value="Upload"/>
                </div>
            </form>
        </div>
    </body>
</html>

0 个答案:

没有答案