获取空的$ _FILES数组

时间:2017-04-12 08:59:11

标签: php html arrays file upload

我想将文件上传到服务器。我已将所有上传文件的代码添加到服务器,但$ _FILES数组为空。文件值未在数组中设置。

我为另一个网页做了相同的代码并且工作正常,但没有弄清楚这个问题。

我已将enctype设置为multipart / form-data但仍为其提供空数组。

html文件:

     <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Post</title>
</head>
<body>

<script>

</script>

<form class="postForm" id="postForm" method="post" action="addPost.php" enctype="multipart/form-data">

    <fieldset>
        <legend>Please add the details below </legend>
        <p>
            <label for="title">Title (required, at least 2 characters)</label>
            <input id="title" name="title" minlength="2" type="text" required>
        </p>

        <p>
            <label for="desc">Description (required, at least 2 characters)</label>
            <input id="desc" name="desc" minlength="2" type="text" required>
        </p>
        <p>
            <label for="keywords">Keywords (eg:#facebook)(required, at least 2 characters)</label>
            <input id="keywords" name="keywords" minlength="2" type="text" required>
        </p>

        <select id="types" name="types" onchange="myFunction(this)">

            <option value="">Select type</option>

            <option value="2">Add Link</option>
            <option value="0">Upload Image</option>
            <option value="1">Upload Video</option>

        </select><br><br>

        <div id="link" style="display: none">

        <p>
            <label for="url">URL (required)</label>
            <input id="url" type="url" name="url" required>
        </p>

        <p>
            <label for="urlType">Select Url Type :(required)</label>
            <select name="urlType" id="urlType">
                <option value="">Select Url Type...</option>
        <!--        <option value="0">Server Image</option>
                <option value="1">Server Video</option>-->
                <option value="2">YouTube Video</option>
                <option value="3">Vimeo Video</option>
                <option value="4">Facebook Image</option>
                <option value="5">Facebook Video</option>
                <option value="6">Instagram Image</option>
                <option value="7">Instagram Video</option>
                <option value="-1">Other</option>
            </select>
        </p>

        </div>

        <div id="filediv" style="display: none">

            Select file to upload:
            <br><br>
            <input name = "file" type="file" id="fileToUpload"><br><br>

        </div>


        <p>
            <label for="postType"> Select Post Type :(required)</label>
            <select name="postType" id="postType">
                <option value="">Select Post Type...</option>
                <option value="0">Normal</option>
                <option value="1">Featured</option>
                <option value="2">Sponsored</option>
            </select>
        </p>
        <p>
            <label for="category"> Select Category :(required)</label>
            <select name="category" id="category">
                <option value="">Select Category...</option>
            </select>
        </p>
        <p>
            <input type="hidden" name="action_type" id="action_type_id"/>
            <input type="hidden" name="id" id="p_id"/>
<!--            <a href="javascript:void(0);" class="btn btn-warning" onclick="$('#postForm').slideUp();">Cancel</a>
            <a href="javascript:void(0);" class="btn btn-success" onclick="userAction('add')">Add User</a>-->
           <input type="submit" name="submit" id="submit" value="Submit">
        </p>
    </fieldset>

    <div class="result" id="result"></div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
    <script>

        function myFunction(obj) {

            var type = obj.value;
            var x = document.getElementById('link');
            var y = document.getElementById('filediv');


            if(type == "2")
            {
                x.style.display = 'block';
                y.style.display = 'none';
            }
            else {
                x.style.display = 'none';
                y.style.display = 'block';
            }

        }

    </script>
</form>
</body>
</html>

addPost.php

    <?php

include 'Database.php';
ini_set('display_errors', 1);
error_reporting(1);
ini_set('error_reporting', E_ALL);

if(isset($_POST['action_type']) && !empty($_POST['action_type'])) {

    if($_POST['action_type'] == 'add') {
         $database = new Database(Constants::DBHOST, Constants::DBUSER, Constants::DBPASS, Constants::DBNAME);
        $dbConnection = $database->getDB();
        $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $dbConnection->prepare("insert into keywords(keyword) 
                                    values(?)");
        $stmt->execute(array($_POST['keywords']));

        $file_result = "";

        if(strcmp($_POST['types'],"2") == 0)
        {

            //insert data into posts table
            $stmt = $dbConnection->prepare("insert into posts(category_id,title,url,url_type,description,keywords,post_type) 
                                    values(?,?,?,?,?,?,?)");
            $stmt->execute(array($_POST['category'], $_POST['title'], $_POST['url'], $_POST['urlType'], $_POST['desc'], $_POST['keywords'],$_POST['postType']));

            $count = $stmt->rowCount();

            if ($count > 0) {

                echo "Post submitted.";
            } else {

                echo "Could not submit post.";
            }


        }
        else {

            if(isset($_POST['submit'])){

                print_r($_FILES);

                if (isset($_FILES["file"]["name"])) {


                    $file_result = "";

                    if ($_FILES["file"]["error"] > 0) {
                        $file_result .= "No file uploaded or invalid file.";
                        $file_result .= "Error code : " . $_FILES["file"]["error"] . "<br>";
                    } else {

                        if (strcmp($_POST['types'], "0") == 0) {
                            $target_dir = "AgTv/images/";
                        } else {
                            $target_dir = "AgTv/videos/";
                        }

                        $newfilename = preg_replace('/\s+/', '',
                            $_FILES["file"]["name"]);
                        $target_file = $target_dir . basename($newfilename);

                        /*$target_file = $target_dir . basename($_FILES["file"]["name"]);*/

                        $file_result .=
                            "Upload " . $_FILES["file"]["name"] . "<br>" .
                            "type " . $_FILES["file"]["type"] . "<br>" .
                            "temp file " . $_FILES["file"]["tmp_name"] . "<br>";


                        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {

                            $stmt = $dbConnection->prepare("insert into posts(category_id,title,url,url_type,description,keywords,post_type) 
                                    values(?,?,?,?,?,?,?)");
                            $stmt->execute(array($_POST['category'], $_POST['title'], $newfilename, $_POST['types'], $_POST['desc'], $_POST['keywords'], $_POST['postType']));

                            $count = $stmt->rowCount();

                            if ($count > 0) {

                                echo "The file " . basename($_FILES['file']['name']) . " has been uploaded, and your information has been added to the directory";

                            } else {

                                echo "Could not submit post.";
                            }

                        }
                    }

                }
                else{

                    echo 'empty file';
                }
            }
        }

    }

}

?>

有人可以帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:1)

更新解决方案: 只需添加

<script>
        $("#postForm").validate();
</script> 

包含jquery.validate.min.js。你$ _FILES数组工作正常,你根本就不发送帖子了!