上传多个文件时如何修复“ ERR_ACCESS_DENIED”

时间:2019-03-28 20:04:27

标签: javascript php jquery http-post

我正在尝试将多个文件上传到服务器。当有一个文件时,它可以正常工作,但是当有多个文件时,我得到“ ERR_ACCESS_DENIED”。不知道这是否与我的权限有关,还是与我的幼稚代码有关。预先感谢!

PHP:

session_start();
function outputJSON($msg, $code = 0, $status = 'error'){
    header('Content-Type: application/json');
    if ($code == 200 || $code == 201){
        echo $code;
        exit();
    }
    echo $msg . "||" . $code . "||" . $status;
    die();
}
//ERROR CODES:
//200 - Upload and Processing successful;
//201 - Upload and Processing successful; - Rename made
//400 - Upload Failed
//511 - Not Authorized
//520 - Unknown
$tempDir = guidv4();
mkdir("disk/processing/" . $tempDir);
$putdir = 'disk/processing/' . $tempDir . '/';

if (isset($_FILES['files']) && !empty($_FILES['files'])) {
    $no_files = count($_FILES["files"]['name']);
    for ($i = 0; $i < $no_files; $i++) {
        if ($_FILES["files"]["error"][$i] > 0) {
            outputJSON($_FILES["files"]["error"], 520, 'error');
        } else {
            if (file_exists($putdir . $_FILES["files"]["name"][$i])) {
                move_uploaded_file($_FILES["files"]["tmp_name"][$i], $putdir . $_FILES["files"]["name"][$i] . ' (1)');
                outputJSON($tempDir, 200, 'done');
            } else {
                move_uploaded_file($_FILES["files"]["tmp_name"][$i], $putdir . $_FILES["files"]["name"][$i]);
                outputJSON($tempDir, 201, 'done');
            }
        }
    }
} else {
    outputJSON('Empty Upload.', 400, 'error');
}

JS / JQ

            $('#information').show();
            var form_data = new FormData();
            var ins = document.getElementById('multiFiles').files.length;
            for (var x = 0; x < ins; x++) {
                form_data.append("files[]", document.getElementById('multiFiles').files[x]);
            }

            $.ajax({
                url: 'uploadbackend.php', // point to server-side PHP script
                dataType: 'application/json', // what to expect back from the PHP script
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,
                type: 'post',
                xhr: function() {
                    var started_at = new Date();
                    xhr.upload.addEventListener( 'progress', function( e )
                    {
                        if( e.lengthComputable )
                        {
                            // Append progress percentage.
                            var loaded = e.loaded;
                            var total = e.total;
                            var progressValue = Math.round( ( loaded / total ) * 100 );

                            // Bytes received.
                            jQuery( '.recievedValue' ).html( '' );
                            jQuery( '.recievedValue' ).append( humanFileSize( loaded ) + ' / ' );

                            // Total bytes.
                            jQuery( '.totalValue' ).html( '' );
                            jQuery( '.totalValue' ).append( humanFileSize( total ) );

                            // Time Remaining
                            var seconds_elapsed =   ( new Date().getTime() - started_at.getTime() )/1000;
                            var bytes_per_second =  seconds_elapsed ? loaded / seconds_elapsed : 0 ;
                            var Kbytes_per_second = bytes_per_second / 1000 ;
                            var remaining_bytes =   total - loaded;
                            var seconds_remaining = seconds_elapsed ? remaining_bytes / bytes_per_second : 'calculating' ;
                            jQuery( '.timeRemaining' ).html( '' );
                            jQuery( '.timeRemaining' ).append( seconds_remaining );

                            // Percentage.
                            $('#_explorer_progress').css("width",  "" + progressValue + "%")
                            if (progressValue >= 100){
                                $('#information').html('Processing...')
                            }

                        }
                    }, false );
                    return xhr;
                },
                success: function (response) {
                    $('#information').html('Done.');
                    $('#upload').html('Uploaded! <i class="fa fa-check ml-5"></i>');
                    $('#upload').removeClass('btn-primary');
                    $('#upload').removeClass('btn-danger');
                    $('#upload').addClass('btn-info');
                    document.getElementById("upload").disabled = true;
                    $('#msg').html(response); // display success response from the PHP script
                    setTimeout(function () {
                        $('#upload').html('Start Upload <i class="fa fa-upload ml-5"></i>');
                        document.getElementById("upload").disabled = false;
                        $('#upload').addClass('btn-primary');
                        $('#upload').removeClass('btn-danger');
                        $('#upload').removeClass('btn-info');
                        $("#multifiles").val('');
                    }, 3000)
                },
                error: function (response) {
                    $('#information').html('There was a problem.');
                    console.log(response);
                }
            });

HTML:

<input type="file" id="multiFiles" name="files[]" multiple="multiple"/>

除了POST请求中的“ ERR_ACCESS_DENIED”错误外,服务器没有其他响应。

只有一个文件时,它会按预期运行。

0 个答案:

没有答案