为什么我的PHP代码为此代码提供了文件未找到错误?

时间:2015-12-14 10:54:34

标签: php file-upload multiple-file-upload

我正在尝试将多个文件发布到服务器。我写了一个完美的演示代码。但是,当我将它与我的主程序集成时;它给我一个文件未找到错误。继承我的代码:

<body>
    <div class="container well" style="margin-top: 50px">
        <form id="profile-form" action="" method="post">
        <div class="row">
            <div class="form-group col-md-2">
                <label style="padding-left:20px;padding-top:10px">Upload image<strong> (5 images required)<strong></label>
            </div>
            <div class="form-group col-md-4">
                <input class="form-control" type="file" name="files[]" id="uploaded_files" accept="png|jpg|jpeg" required="required" multiple>
            </div>
        </div>
        </form>
</body>

这是我的PHP代码..

<?php
if(isset($_POST['submit'])){

        $firstName = trim($_POST['name']);
        $middleName = trim($_POST['middleName']);

        $uploadedImage = array();
        $uploadedImageName = array();

        if(isset($_FILES['files']['tmp_name'])){

            $num_files = count($_FILES['files']['tmp_name']);
            echo $num_files;


            if($num_files == 5){
                for($i = 0; $i < $num_files; $i++){

                    $imgName = addslashes($_FILES['files']['tmp_name'][$i]);
                    $name = addslashes($_FILES['files']['name'][$i]);

                    if($_FILES['files']['tmp_name'][$i] != ""){

                        $imageContents = file_get_contents($imgName);
                        $encodedImage[$i] = base64_encode($imageContents);

                        $filename[$i] = $_FILES['files']['name'][$i];

                        array_push($uploadedImageName, $filename[$i]);
                        array_push($uploadedImage, $encodedImage[$i]);

                        echo "I got the file..<br>";

                        echo ",,".$filename[$i];
                        echo $encodedImage[$i];
                    }   
                }
            } else{
                echo "Number of files should be equal to 5";
                return;
            }

        } else{
            echo "Files not found..";
        }
$mysqli = new mysqli("localhost", "root", "", "people");

        if(mysqli_connect_error()){
            echo mysqli_connect_error();
            exit;
        } 

// insert and execute query
?>

1 个答案:

答案 0 :(得分:2)

您的表单标记中缺少enctype="multipart/form-data">

您的form元素将是

<form id="profile-form" action="" method="post" enctype="multipart/form-data">