doubo4336 2016-10-03 15:47
浏览 27

多图片上传问题

I have a simple script that can upload files on my server and insert the details into database.

With code below I am getting two errors..

  1. I tried to allow only .jpg. gif and .png formats but does not work.. All kind of formats are uploaded on server..

  2. Script import numbers (1,2,3...) into Mysql if the upload filed is empty..

I tried for hours to fix these issues but I dont know whats wrong..

Thank you..

 <?php

      include_once('connect.php');

        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_type = $_FILES['files']['type'][$key];

                if($file_type == "image/gif"){
                    $sExt = ".gif";
                } elseif($file_type == "image/jpeg" || $file_type == "image/pjpeg"){
                    $sExt = ".jpg";
                } elseif($file_type == "image/png" || $file_type == "image/x-png"){
                    $sExt = ".png";
                }
                if (!in_array($sExt, array('.gif','.jpg','.png'))) {
                    $errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
                }
                $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';
                }       
                $query = "INSERT into offers_pics (`offer_id`,`pic_name`,`pic_type`) VALUES ('$user_id','$file_name','$file_type'); ";
                $result = mysqli_query($link,$query);

                $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) ;               
                    }
                }else{
                        print_r($errors);
                }
            }
            if(empty($error)){
                echo "Success";
            }
        }
        ?>


    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="files[]"> <br/>
        <input type="file" name="files[]"> <br/>
        <input type="file" name="files[]"> <br/>
        <input type="file" name="files[]"> <br/>
        <input type="file" name="files[]" > <br/><br/>
        <input type="submit"/>
    </form>

    enter code here
  • 写回答

1条回答 默认 最新

  • dsd119120 2016-10-03 16:01
    关注

    Fo the first error, the errors array is outside the foreah so you doesn't clear the error for each file, and the query to insert is outside the if that checks errors so is always ecexcuted

    For the second error the same, the query is executed always you have to check if the file is uploaded.

    <?php
    
        include_once('connect.php');
    
        if(isset($_FILES['files'])){
            $filesErrors = 0;
            foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
                // MOved errors inside the foreach to clear it each loop
                $errors = array();
    
                // Check file is uploaded
                if ($_FILES['files']['error'][$key] == UPLOAD_ERR_NO_FILE){
                    // Continue with the next file
                    continue; 
                }
    
                $file_name = $key.$_FILES['files']['name'][$key];
                $file_size = $_FILES['files']['size'][$key];
                $file_type = $_FILES['files']['type'][$key];
    
                if($file_type == "image/gif"){
                    $sExt = ".gif";
                } elseif($file_type == "image/jpeg" || $file_type == "image/pjpeg"){
                    $sExt = ".jpg";
                } elseif($file_type == "image/png" || $file_type == "image/x-png"){
                    $sExt = ".png";
                }
                if (!in_array($sExt, array('.gif','.jpg','.png'))) {
                    $errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
                }
    
                $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';
                }    
    
    
                if(empty($errors) == true){
                    $desired_dir = "user_data";
                    // Execute query inside the errors check
                    $query = "INSERT into offers_pics (`offer_id`,`pic_name`,`pic_type`) VALUES ('$user_id','$file_name','$file_type'); ";
                    $result = mysqli_query($link,$query);
    
                    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) ;               
                    }
                }else{
                    $filesErrors++;
                    print_r($errors);
                }
            }
    
            if ($filesErrors == 0){
                echo 'Success';
            }
        }
    ?>
    
    
    <form action="" method="POST" enctype="multipart/form-data">
        <input type="file" name="files[]"> <br/>
        <input type="file" name="files[]"> <br/>
        <input type="file" name="files[]"> <br/>
        <input type="file" name="files[]"> <br/>
        <input type="file" name="files[]" > <br/><br/>
        <input type="submit"/>
    </form>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比