多个图片上传问题 [英] Multiple Picture Upload Problems

查看:65
本文介绍了多个图片上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的脚本,可以将文件上传到服务器上,并将详细信息插入数据库中.

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. 我尝试仅允许.jpg. gif和.png格式,但不起作用.所有类型的格式均已上传到服务器上.

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

如果上传字段为空,则脚本导入编号(1,2,3 ...)到Mysql中..

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..

谢谢..

 <?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

推荐答案

针对第一个错误,errors数组位于前景色之外,因此您无需清除每个文件的错误,并且要插入的查询不在if之外检查错误,因此总是被执行

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>

这篇关于多个图片上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆