文件上传失败,错误为“ 503,网关错误”

时间:2019-12-17 21:32:49

标签: javascript php jquery ajax http-status-code-503

我正在尝试使用表单上传文件。每当我尝试使用本地服务器上传时,它都可以正常运行。但是,当我在在线应用程序上尝试此操作时,它通常会失败并显示“错误503,网关错误”。不知道出了什么问题! 服务器功能是

Memory Limit:256M
Max Upload Size:512M
Max Post Size:512M
Max Exe Time:300
Max Inp Time:300

请帮助我摆脱这个问题!!!

这是代码

HTML表单

<form class="contact-form" id="upload_form" action="#" method="post" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-md-12">
                            <label><small>License Type<sup title="Necessary">(*)</sup></small></label>
                            <?php
                                $fetch_types = $connection->query("SELECT * FROM `wo_license_types` WHERE `status` = 1");
                                if($fetch_types){
                                    if($fetch_types->num_rows > 0){ ?>
                                        <select name="license_type" required required="required">
                                            <?php
                                                while($type = $fetch_types->fetch_assoc()){
                                                    ?>
                                                        <option value="<?php echo $type['id']; ?>"><?php echo $type['title']; ?></option>
                                                    <?php
                                                }
                                            ?>
                                        </select>
                                    <?php } else { ?>
                                        <input type="text" name="license_type" value="<?php echo getPost('license_type'); ?>" placeholder="License Type">
                                    <?php }
                                } else {
                                    redirect('lessons.php');
                                }
                            ?>
                        </div>
                        <div class="col-md-6">
                            <label><small>Chapter No<sup title="Necessary">(*)</sup></small></label>
                            <input type="text" name="chapter_no" required required="required" placeholder="Chapter No" pattern="[0-9]{1,3}" title="The chapter number should only contain numbers from 0 to 9 i.e. 12">
                        </div>
                        <div class="col-md-6">
                            <label><small>Chapter Name<sup title="Optional">(Opt)</sup></small></label>
                            <input type="text" name="chapter_title" placeholder="Chapter Name">
                        </div>
                        <div class="col-md-6">
                            <label><small>Page No<sup title="Necessary">(*)</sup></small></label>
                            <input type="text" name="page_no" required required="required" placeholder="Page No" pattern="[0-9]{1,3}" title="The page number should only contain numbers from 0 to 9 i.e. 120">
                        </div>
                        <div class="col-md-12">
                            <label><small>Description<sup title="Optional">(Opt)</sup></small></label>
                            <textarea name="description" placeholder="Give brief description"></textarea>
                        </div>
                        <div class="col-md-6">
                            <label><small>Attach Audio<sup title="Necessary">(*)</sup></small></label>
                            <input type="file" name="audio" id="audio" required required="required" placeholder="Attach audio file" style="padding-top: 15px;">
                        </div>
                        <div class="col-md-6">
                            <label><small>Attach Picture<sup title="Necessary">(*)</sup></small></label>
                            <input type="file" name="img" id="img" required required="required" placeholder="Attach picture" style="padding-top: 15px;">
                        </div>
                        <div class="col-sm-12">
                            <progress id="audio_progressBar" value="0" max="100" style="width:100%;"></progress>
                            <small id="audio_status"></small>
                        </div>
                        <div class="col-md-12" style="text-align: center;">
                            <input style="background-color: #f43f00; color: #ffffff; margin-bottom: 0px; padding-bottom: 0px;" type="button" id="post_lesson_button" value="Upload Lesson" onclick="post_lesson()">
                        </div>
                    </div>
                </form>

Java脚本:

<script>
    function _(el){
        return document.getElementById(el);
    }

    function post_lesson(){
        document.getElementById("post_lesson_button").disabled = true;
        _("audio_status").innerHTML = "<span style='color: blue;'><b>Progress:</b> Posting Lesson...</span>";
        var license_type    = $("select[name='license_type']").val().trim();
        var chapter_no      = $("input[name='chapter_no']").val().trim();
        var chapter_title   = $("input[name='chapter_title']").val().trim();
        var page_no         = $("input[name='page_no']").val().trim();
        var description     = $("textarea[name='description']").val().trim();
        var img_file        = _("img");
        var audio_file      = _("audio");
        if(img_file.files.length > 0 & audio_file.files.length > 0){
            var formdata    = new FormData();
            formdata.append("http_request", "post_lesson");
            formdata.append("license_type", license_type);
            formdata.append("chapter_no", chapter_no);
            formdata.append("chapter_title", chapter_title);
            formdata.append("page_no", page_no);
            formdata.append("description", description);
            formdata.append("img", img_file.files[0]);
            formdata.append("audio", audio_file.files[0]);
            var ajax = new XMLHttpRequest();
            ajax.upload.addEventListener("progress", progressHandler, false);
            ajax.addEventListener("load", completeHandler, false);
            ajax.addEventListener("error", errorHandler, false);
            ajax.addEventListener("abort", abortHandler, false);
            ajax.open("POST", "file_upload_parser.php", true);
            ajax.send(formdata);
        } else {
            _("audio_status").innerHTML = "<span style='color: red;'><b>Error: </b>Please attach image and audio files.</span>";
            document.getElementById("post_lesson_button").disabled = false;
        }
    }

    function progressHandler(event){
        var percent = (event.loaded / event.total) * 100;
        _("audio_progressBar").value = Math.round(percent);
        _("audio_status").innerHTML = Math.round(percent)+"% uploading... please wait";
    }
    function completeHandler(event){
        if(event.target.responseText[event.target.responseText.length-1] == 1){
            _("audio_status").innerHTML = "<span><b style='color: green;'>Success:</b> Lesson posted successfully !</span>";
            _("upload_form").reset();
            document.getElementById("post_lesson_button").disabled = false;
            window.location.href = "add-lesson.php";
        } else {
            _("audio_status").innerHTML = event.target.responseText;
            document.getElementById("post_lesson_button").disabled = false;
        }
    }
    function errorHandler(event){
        _("audio_status").innerHTML = "Upload Failed";
        document.getElementById("post_lesson_button").disabled = false;
        console.log(event.target.responseText);
        console.log(event);
    }
    function abortHandler(event){
        _("audio_status").innerHTML = "Upload Aborted";
        document.getElementById("post_lesson_button").disabled = false;
        console.log(event.target.responseText);
        console.log(event);
    }

</script>

file_upload_parser.php

    <?php
    require_once 'core/init.php';

    if(isset($_POST['http_request']) AND getPost('http_request') == 'post_lesson'){
        $license_type   = getPost('license_type');
        $chapter_no     = getPost('chapter_no');
        $chapter_title  = getPost('chapter_title');
        $page_no        = getPost('page_no');
        $description    = getPost('description');
        $img_file       = $_FILES["img"];
        $audio_file     = $_FILES['audio'];
        $chapter_id     = null;
        $img_upl_name   = null;
        $audio_upl_name = null;
        if(is_numeric($chapter_no) AND $chapter_no > 0 AND is_numeric($page_no) AND $page_no > 0 AND ((strlen($chapter_title) >= 3 AND strlen($chapter_title) <= 55) OR (strlen($chapter_title) == 0)) AND ((strlen($description) >= 10 AND strlen($description) <= 500) OR (strlen($description) == 0))){
            if($error_status == false){
                echo "<span><b style='color: blue;'>Progress:</b> Validating data...<br /></span>";
                $verify_license_type = $connection->query("SELECT * FROM `wo_license_types` WHERE `id` = '$license_type' AND `status` = 1");
                if(!$verify_license_type OR $verify_license_type->num_rows != 1){
                    echo "<span style='color: red;'>Something went wrong with license type !</span>";
                    exit();
                }
                $verify_chapter = $connection->query("SELECT * FROM `wo_license_chapters` WHERE `license_type` = '$license_type' AND `chapter_no` = '$chapter_no' AND `status` = 1");
                if($verify_chapter){
                    if($verify_chapter->num_rows == 0){
                        $add_chapter = $connection->query("INSERT INTO `wo_license_chapters` (`chapter_no`, `title`, `license_type`, `added_by`, `status`) VALUES ('$chapter_no', '$chapter_title', '$license_type', '$admin_id', 1)");
                        if($add_chapter){
                            $chapter_id = $connection->insert_id;
                        } else {
                            echo "<span style='color: red;'><b>Error:</b> Oops... an unknown error occurred !</span>";
                            exit();
                        }
                    } else if($verify_chapter->num_rows == 1){
                        $chapter_id = $verify_chapter->fetch_assoc()['id'];
                    } else {
                        echo "<span style='color: red;'><b>Error:</b> Oops... an unknown error occurred !</span>";
                        exit();
                    }
                } else {
                    echo "<span style='color: red;'><b>Error:</b> Oops... an unknown error occurred !</span>";
                    exit();
                }
            } else {
                echo "<span style='color: red;'><b>Error:</b> Oops... an unknown error occurred !</span>";
                exit();
            }

            if($error_status == false){
                $verify_page_no = $connection->query("SELECT * FROM `wo_license_pages` WHERE `chapter_id` = '$chapter_id' AND `page_no` = '$page_no' AND `language` = 1 AND `status` = 1");
                if($verify_page_no){
                    if($verify_page_no->num_rows == 1){
                        $error_status = true;
                        echo "<span style='color: red;'>You have already added this page.</span>";
                        exit();
                    }
                } else {
                    echo "<span style='color: red;'><b>Error:</b> Oops... an unknown error occurred !</span>";
                    exit();
                }
            } else {
                echo "<span style='color: red;'><b>Error:</b> Oops... an unknown error occurred !</span>";
                exit();
            }
            if($error_status == false){
                echo "<span><b style='color: blue;'>Progress:</b> Validating files...<br /></span>";
                $img_name       = $img_file["name"];
                $img_TmpLoc     = $img_file["tmp_name"];
                $img_ErrorMsg   = $img_file["error"];
                $img_Size       = $img_file['size'];
                if(!$img_TmpLoc OR empty($img_name)) {
                    echo "<span><b>Error:</b> Please browse for an image file before clicking the post button.</span>";
                    $error_status = true;
                    exit();
                }
                if($img_Size >= 10485760){
                    echo "<span><b>Error:</b> Image file is larger than 10 MB, Please choose other file.</span>";
                    $error_status = true;
                    exit();
                }
                $allowed_img_files  = array('jpg', 'jpeg', 'png');
                $img_ext            = explode('.', $img_name);
                $img_ext            = end($img_ext);
                $img_upl_name       = uniqid() . '.' . $img_ext;
                if(!in_array($img_ext, $allowed_img_files)){
                    echo "<span style='color: red;'><b>Error:</b> Please choose a valid image file.</span>";
                    $error_status = true;
                    exit();
                }
                $audio_name         = $audio_file["name"];
                $audio_TmpLoc       = $audio_file["tmp_name"];
                $audio_ErrorMsg     = $audio_file["error"];
                $audio_Size         = $audio_file['size'];
                if(!$audio_TmpLoc OR empty($audio_name)) {
                    echo "<span><b style='color: red;'>Error:</b> Please browse for an audio file before clicking the post button.</span>";
                    $error_status = true;
                    exit();
                }
                if($audio_Size >= 125829120){
                    echo "<span><b style='color: red;'>Error:</b> Audio file is larger than 120 MB, Please choose other file.</span>";
                    $error_status = true;
                    exit();
                }
                $allowed_audio_files    = array('mp3', 'aac', 'wav');
                $audio_ext              = explode('.', $audio_name);
                $audio_ext              = end($audio_ext);
                $audio_upl_name         = uniqid() . '.' . $audio_ext;
                if(!in_array($audio_ext, $allowed_audio_files)){
                    echo "<span style='color: red;'><b>Error:</b> Please choose a valid audio file.</span>";
                    $error_status = true;
                    exit();
                }
                if($error_status == false){
                    echo "<span><b style='color: blue;'>Progress:</b> Uploading image file...<br /><span>";
                    $img_upl_path = "../files/lessons/imgs/".$img_upl_name;
                    if(!move_uploaded_file($img_TmpLoc, $img_upl_path)){
                        echo "<span><b style='color: red;'>Failed to upload image file, please try again !</b></span>";
                        $error_status = true;
                        exit();
                    }
                    echo "<span><b style='color: blue;'>Progress:</b> Uploading audio file...<br /><span>";
                    $audio_upl_path = "../files/lessons/audios/".$audio_upl_name;
                    if(!move_uploaded_file($audio_TmpLoc, $audio_upl_path)){
                        echo "<span><b style='color: red;'>Failed to upload audio file, please try again !</b></span>";
                        $error_status = true;
                        exit();
                    }
                    if($error_status == false){
                        echo "<span><b style='color: blue;'>Progress:</b> Posting Lesson...<br /></span>";
                        $audio_upl_name = array($audio_upl_name);
                        $audio_upl_name = json_encode($audio_upl_name);
                        $post_lesson    = $connection->query("INSERT INTO `wo_license_pages` (`page_no`, `description`, `img`, `audio`, `chapter_id`, `language`, `added_by`, `status`) VALUES ('$page_no', '$description', '$img_upl_name', '$audio_upl_name', '$chapter_id', 1, '$admin_id', 1)");
                        if($post_lesson){
                            echo "1";
                            exit();
                        } else {
                            echo "<span><b style='color: red;'>Error:</b> Failed to post lesson.</span>";
                            exit();
                        }
                    } else {
                        echo "<span><b style='color: red;'>Error:</b> Failed to post the lesson.</span>";
                        exit();
                    }
                } else {
                    echo "<span><b style='color: red;'>Error:</b> Failed validation</span>";
                    exit();
                }
            } else {
                echo "<span style='color: red;'>Failed validation !</span>";
                exit();
            }
        } else {
            $error_status = true;
            if(!is_numeric($chapter_no)){
                echo "<span><b style='color: red;'>Error:</b> Enter a valid chapter number.</span>";
            } else if($chapter_no <= 0){
                echo "<span><b style='color: red;'>Error:</b> Enter a valid chapter number.</span>";
            } else if(!is_numeric($page_no)){
                echo "<span><b style='color: red;'>Error:</b> Enter a valid page number.</span>";
            } else if($page_no <= 0){
                echo "<span><b style='color: red;'>Error:</b> Enter a valid page number.</span>";
            } else if(strlen($chapter_title) > 0 AND strlen($chapter_title) < 3){
                echo "<span><b style='color: red;'>Error:</b> Chapter name must contain atleast 3 characters.</span>";
            } else if(strlen($chapter_title) > 55){
                echo "<span><b style='color: red;'>Error:</b> Chapter name can contain maximum 55 characters.</span>";
            } else if(strlen($description) > 0 AND strlen($description) < 10){
                echo "<span><b style='color: red;'>Error:</b> Demo Description must contain at least 10 characters.</span>";
            } else if(strlen($description) > 500){
                echo "<span><b style='color: red;'>Error:</b> Demo Description can contain maximum 500 characters.</span>";
            } else {
                echo "<span><b style='color: red;'>Error:</b> Oops... An unknown error occurred !</span>";
            }
        }
    }
?>

0 个答案:

没有答案