将多个文件上传到位置并更新数据库

时间:2018-11-25 07:43:59

标签: php mysql

我一直在研究似乎无法完成的多图像上传功能。目前,仅当我上传了一张图像时,它才有效。我无法弄清楚此脚本出了什么问题。

这是上传图像的功能:

    if(isset($_POST["btnSubmit"])){     
        $errors = array();

        $extension = array("jpeg","jpg","png","gif");

        $bytes = 1000000;
        $allowedKB = 10485760000;
        $totalBytes = $allowedKB * $bytes;

        $imgDir = "assets/users/".$userLoggedIn."/images/";

        if(isset($_FILES["files"])==false)
        {
            echo "<b>Please, Select the files to upload!!!</b>";
            return;
        }

        foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
        {
            $uploadThisFile = true;

            $file_name=$_FILES["files"]["name"][$key];
            $file_tmp=$_FILES["files"]["tmp_name"][$key];

            $ext=pathinfo($file_name,PATHINFO_EXTENSION);

            if(!in_array(strtolower($ext),$extension))
            {
                array_push($errors, "File type is invalid. Name:- ".$file_name);
                $uploadThisFile = false;
            }               

            if($_FILES["files"]["size"][$key] > $totalBytes){
                array_push($errors, "File size is too big. Name:- ".$file_name);
                $uploadThisFile = false;
            }

            if($uploadThisFile){
                $filename = basename($file_name,$ext);
                $newFileName = uniqid().$filename.$ext;

                move_uploaded_file($_FILES["files"]["tmp_name"][$key],$imgDir.$newFileName);

                // current date and time
                $date_added = date("Y-m-d H:i:s");

                $imagePath = $imgDir.$newFileName;

                $query = "INSERT INTO images(date_added, added_by, image, deleted) VALUES('$date_added', '$userLoggedIn','$imagePath', 'no')";

                mysqli_query($con, $query);     
            }
        }

        header("Location: edit-images.php");

        $count = count($errors);

        if($count != 0){
            foreach($errors as $error){
                echo $error."<br/>";
            }
        }       
    }
?>

我认为问题可能出在尺寸上,但我降低了条件以防止它没有运气上传。我觉得我可能在foreach中缺少某些内容,但是我完全陷入了困境。感谢您的帮助!

这是我要上传的表格:

<form method="post" enctype="multipart/form-data" name="formUploadFile" id="uploadForm" action="edit-images.php">
            <div class="form-group">
                <label for="exampleInputFile">Select file to upload:</label>
                <input type="file" id="exampleInputFile" name="files[]" multiple="multiple">
                <p class="help-block"><span class="label label-info">Note:</span> Please, Select the only images (.jpg, .jpeg, .png, .gif)</p>
            </div>          
            <button type="submit" class="btn btn-primary" name="btnSubmit" >Start Upload</button>
        </form>

0 个答案:

没有答案