多个图片上传有问题

时间:2016-07-04 11:23:44

标签: php image-uploading

我需要编写一个代码,用户可以一次上传多个图像,我已经尝试了一些代码,但遗憾的是它显示了“解析错误:语法错误,意外'}'”对于多个上传概念来说是个新手请帮我解决这个问题,请原谅我,如果我在某处出错了,谢谢。

form.php的

<div class="modal-body">
     <form action="multi_upload.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="files[]" multiple/><br>
</div>
<div class="modal-footer">
        <button type="button" class="btn btn-white" data-dismiss="modal">Close</button>
        <input type="submit" name="files" class="btn btn-info" value="Upload"/>
     </form>
</div>

upload.php的

<?php
 session_start();
    define('HOST','localhost');
    define('USER','***');
    define('PASS','***');
    define('DB','***');

    $response = array();

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

if(isset($_FILES['files'])){
    $errors= array();
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size =$_FILES['files']['size'][$key];
        $file_tmp =$_FILES['files']['tmp_name'][$key];
        $file_type=$_FILES['files']['type'][$key];  
        if($file_size > 2097152){
            $errors[]='File size must be less than 2 MB';
        }       
        $sql ="INSERT into user_gallery (`vault_no`,`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`, created_at) VALUES('".$_SESSION['vault_no']."','$file_name','$file_size','$file_type', NOW())";
        $desired_dir="user_data";
        if(empty($errors)==true){
            if(is_dir($desired_dir)==false){
                mkdir("$desired_dir", 0700);        // Create directory if it does not exist
            }
            if(is_dir("$desired_dir/".$file_name)==false){
                move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
            }else{                                  // rename the file if another one exist
                $new_dir="$desired_dir/".$file_name.time();
                 rename($file_tmp,$new_dir) ;               
            }
         mysqli_query($con,$sql)        
        }else{
                print_r($errors);
        }
    }
    if(empty($error)){
        echo "Success";
    }
}
?>

1 个答案:

答案 0 :(得分:2)

;

之后,你错过了mysqli_query($con,$sql)