【问题标题】:Wrong location of conditional statements. Because of loop everything is destroyed, using multi upload files条件语句的位置错误。由于循环一切都被破坏,使用多上传文件
【发布时间】:2016-03-12 10:01:28
【问题描述】:

每次上传新文件时,此应用程序都会创建一个新文件夹,并将这些文件插入到创建的文件夹中。 下面的代码工作正常,但我有点困惑。由于多次上传,我无法检查是否上传了某些内容 我失去了对创建文件夹的控制,因为即使我不上传文件(仅提交表单)也会创建它。 为什么我只提交表单时会创建文件夹?因为“UPLOAD_ERR_OK”在创建文件夹后检查表单(首先是“mkdir”,然后是“UPLOAD_ERR_OK”(但必须这样,因为循环([$i]!)))。 我想做的是把 UPLOAD_ERR_OK 放在开头,但我不能,因为它必须包含一个数组 [$i]!在单次上传中“if ($_FILES['img']['error'] == UPLOAD_ERR_OK)”工作正常,但是 不在 multi 中,因为数组。

下面的脚本有阶段:

  1. 创建文件夹
  2. 如果文件没有错误
  3. 上传文件到文件夹

这是错误的,因为即使我们上传空表单并检查表单,也会在开始时创建文件夹,所以它会创建一个文件夹并报错“UPLOAD_ERR_NO_FILE”。但是不可能先检查错误再创建文件夹,因为错误是在循环后检查数组。

if (!file_exists($pre_path)) // if no folder
{
    if (mkdir($pre_path, 0777)) // create folder
    {
        for ($i=0; $i < count($_FILES['img']['name']) $i++) //LOOP
        {
            if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK) //if no error
            {
                //MOVE UPLOADED FILES[$i], QUERIES AND FUNCTIONS
            }
            elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { }//show error
            elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) {} //show error
            elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { } //show error
            elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) {}  //show error
            elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { } //show error
            elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { } //show error
            elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { }//show error
            else { }//show error
        }
    }
}

我希望它是这样的: 1.如果文件没有错误(不是“UPLOAD_ERR_NO_FILE”) 2.创建文件夹 3. 上传文件到文件夹

if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK)
{
if (mkdir($pre_path, 0777))
{
    for ($i=0; $i < count($_FILES['img']['name']) $i++)
    {
        //MOVE UPLOADED FILES[$i], QUERIES AND FUNCTIONS
    }
}
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { }//show error
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) {}  //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { }//show error
else { }//show error

我应该怎么做才能将“if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK)” 与 [$i] 数组放在一起并在 errros 之后创建一个文件夹被检查?我们不能将 mkdir 放在循环中,因为循环会创建很多文件夹。我只需要 1 个文件夹,所以它必须在循环之前。 :/ 太疯狂了。 完整代码:

if (isset($_SESSION['admin'], $_POST['upload_images']))
{
    $img_tmp_name = $_FILES['img']['tmp_name'];
    $img_name = $_FILES['img']['name'];
    $img_error = $_FILES['img']['error'];
    $img_type = $_FILES['img']['type'];
    $img_size = $_FILES['img']['size'];

    $image_quantity = count($img_name);

    $error_text = array (
        1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
        2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
        3 => 'The uploaded file was only partially uploaded',
        4 => 'No file was uploaded',
        6 => 'Missing a temporary folder',
        7 => 'Failed to write file to disk.',
        8 => 'A PHP extension stopped the file upload.',
        9 => 'file couldnt be moved!',
        10 => 'file isnt uploaded',
        11 => 'za duzy rozmiar pliku',
        12 => 'obrazek musi byc w formacie JPEG',
        13 => 'nie mozna stworzyc folderu',
        14 => 'Query has not started, but folder has created with number: ',
        15 => 'Unknown upload error'
    );

    $conn = lacz_bd();

    if ($q = $conn->prepare('SELECT MAX(gallery_id) FROM galleries'))
    {
        $q->execute();
        $q->bind_result($gallery_id);
        $q->store_result();

        while($q->fetch())
        {
            $improved_gallery_id = $gallery_id + 1;
        }

        $pre_path = ('./galerie/'.$improved_gallery_id);

        //------------------------------- START UPLOAD ----------------------------------   

        if (!file_exists($pre_path))
        {
            if (mkdir($pre_path, 0777))
            {
                for ($i=0; $i < $image_quantity; $i++)
                {
                    if ($img_error[$i] == UPLOAD_ERR_OK)
                    {
                        if ($img_type[$i] = 'image/jpeg') //to do: preg match instead
                        {
                            if ($img_size[$i] <= 2621440) //2,5MiB
                            {
                                if (is_uploaded_file($img_tmp_name[$i]))
                                {
                                    $path = $pre_path.'/'.basename($img_name[$i]);

                                    if (move_uploaded_file($img_tmp_name[$i], $path))
                                    {
                                        if ($q = $conn->prepare('INSERT INTO galleries (path, gallery_id) VALUES (?, ?)'))
                                        {
                                            $q->bind_param('si', $path, $improved_gallery_id);
                                            $q->execute();

                                            if ($q->affected_rows > 0)
                                            {
                                                image_compression($path, 800, 536);

                                                echo '<a href="ustaw_miniature.php?m='.urlencode($img_name[$i]).'&gid='.urlencode($improved_gallery_id).'">';
                                                echo '<img src="'. htmlspecialchars($path, ENT_QUOTES).'" width="80%" height="80%"></img></a><br />';                    
                                            }
                                        }
                                    } else { echo $error_text[9]; }
                                } else { echo $error_text[10]; }
                            } else { echo $error_text[11]; }
                        } else { echo $error_text[12]; }
                    }
                    elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { echo $error_text[1]; }
                    elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) { echo $error_text[2]; }
                    elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { echo $error_text[3]; }
                    elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) { echo $error_text[4]; }
                    elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { echo $error_text[6]; }
                    elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { echo $error_text[7]; }
                    elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { echo $error_text[8]; }
                    else { echo $error_text[15]; }
                } // END FOR
            }

        } else { echo $error_text[14].' '.htmlspecialchars($improved_gallery_id, ENT_QUOTES);}

        $q->free_result();
        $q->close();
    }
    $conn->close();
}

我坐在电脑前等待帮助。谢谢。这对我很重要,因为我为开发人员制作了一个网站。

【问题讨论】:

    标签: php arrays loops file-upload mkdir


    【解决方案1】:

    您可以重新组织条件,使其仅在第一个循环 $i == 0 上检查和创建文件夹。您需要检查并可能更改以下示例中的错误消息 $error_text[14],这是更改条件的一种方法。

    if (isset($_SESSION['admin'], $_POST['upload_images']))
    {
        $img_tmp_name = $_FILES['img']['tmp_name'];
        $img_name = $_FILES['img']['name'];
        $img_error = $_FILES['img']['error'];
        $img_type = $_FILES['img']['type'];
        $img_size = $_FILES['img']['size'];
    
        $image_quantity = count($img_name);
    
        $error_text = array (
            1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
            2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
            3 => 'The uploaded file was only partially uploaded',
            4 => 'No file was uploaded',
            6 => 'Missing a temporary folder',
            7 => 'Failed to write file to disk.',
            8 => 'A PHP extension stopped the file upload.',
            9 => 'file couldnt be moved!',
            10 => 'file isnt uploaded',
            11 => 'za duzy rozmiar pliku',
            12 => 'obrazek musi byc w formacie JPEG',
            13 => 'nie mozna stworzyc folderu',
            14 => 'Query has not started, but folder has created with number: ',
            15 => 'Unknown upload error'
        );
    
        $conn = lacz_bd();
    
        if ($q = $conn->prepare('SELECT MAX(gallery_id) FROM galleries'))
        {
            $q->execute();
            $q->bind_result($gallery_id);
            $q->store_result();
    
            while($q->fetch())
            {
                $improved_gallery_id = $gallery_id + 1;
            }
    
            $pre_path = ('./galerie/'.$improved_gallery_id);
    
            //------------------------------- START UPLOAD ----------------------------------   
    
    
            for ($i=0; $i < $image_quantity; $i++)
            {
                if ($img_error[$i] == UPLOAD_ERR_OK)
                {
                    if ($img_type[$i] = 'image/jpeg') //to do: preg match instead
                    {
                        if ($img_size[$i] <= 2621440) //2,5MiB
                        {
                            if (is_uploaded_file($img_tmp_name[$i]))
                            {
                                if ($i == 0) // only the first loop create directory
                                {
                                    if (!file_exists($pre_path) && !is_dir($pre_path))
                                    {
                                        if (mkdir($pre_path, 0777))
                                        {
                                            // todo: handle the error
                                            exit();
                                        }
                                    }
                                }
                                if (is_dir($pre_path))
                                {
                                    $path = $pre_path.'/'.basename($img_name[$i]);
    
                                    if (move_uploaded_file($img_tmp_name[$i], $path))
                                    {
                                        if ($q = $conn->prepare('INSERT INTO galleries (path, gallery_id) VALUES (?, ?)'))
                                        {
                                            $q->bind_param('si', $path, $improved_gallery_id);
                                            $q->execute();
    
                                            if ($q->affected_rows > 0)
                                            {
                                                image_compression($path, 800, 536);
    
                                                echo '<a href="ustaw_miniature.php?m='.urlencode($img_name[$i]).'&gid='.urlencode($improved_gallery_id).'">';
                                                echo '<img src="'. htmlspecialchars($path, ENT_QUOTES).'" width="80%" height="80%"></img></a><br />';                    
                                            } // end affected_rows
                                        } // end prepare
                                    } // end move_uploaded_file
                                    else { echo $error_text[9]; }
                                } // end is_dir
                                else { echo $error_text[14].' '.htmlspecialchars($improved_gallery_id, ENT_QUOTES);}
                            } // end is_uploaded_file
                            else { echo $error_text[10]; }
                        } // end image size
                        else { echo $error_text[11]; }
                    } // end image type
                    else { echo $error_text[12]; }
                } // end error check
                elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { echo $error_text[1]; }
                elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) { echo $error_text[2]; }
                elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { echo $error_text[3]; }
                elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) { echo $error_text[4]; }
                elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { echo $error_text[6]; }
                elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { echo $error_text[7]; }
                elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { echo $error_text[8]; }
                else { echo $error_text[15]; }
            } // end for loop
    
            $q->free_result();
            $q->close();
        } // end prepare
        $conn->close();
    } // end isset
    

    【讨论】:

    • 你好,特里斯坦,它工作得很好!你救了我的命!!!我刚刚读到你是一名老师,祝你和你的孩子好运! :) 非常感谢您。我想给你买一块巧克力,但我住在波兰。 ;) 祝你有美好的一天,特里斯坦。
    【解决方案2】:

    您可以使用函数重新排列代码。你的代码真的很难理解。 首先你可以上传文件到临时文件夹,然后调用函数检查,如果一切正常,你可以调用函数准备文件夹(如果不存在则创建),然后将文件从临时文件夹复制到新文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      相关资源
      最近更新 更多