php 警告:mkdir():文件存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22032345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:37:35  来源:igfitidea点击:

Warning: mkdir(): File exists

php

提问by clien

The file transferring to my upload folder is working well but I have a warning in mkdir. It says file exist but the picture and folder generates own name. I don't know what warning is determining.

传输到我的上传文件夹的文件运行良好,但我在mkdir 中有警告。它说文件存在但图片和文件夹生成自己的名称。我不知道什么警告正在确定。

Anyone can help me?

任何人都可以帮助我吗?

include('connect.php');

$dir=substr(uniqid(),-7); // Uniqid for subdirectory

$path = "uploads/$dir/"; // uploads/subdirectory/  // Make directory

$valid_formats = array("jpg", "png", "jpeg", "kml");

$max_file_size = 2097152;

$count = 0;


// Loop $_FILES to execute all files

 if(!empty($_FILES)){
foreach($_FILES['files']['name'] as $f => $name) {

    if ($_FILES['files']['error'][$f] == 4) {
        continue; // Skip file if any error found
    }  

    if ($_FILES['files']['error'][$f] == 0) {              
        if ($_FILES['files']['size'][$f] > $max_file_size) {
            $message[] = "$name is too large!.";
            continue; // Skip large files
        }

        elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
            $message[] = "$name is not a valid format";
            continue; // Skip invalid file formats
        }

        else{ // No error found! Move uploaded files 
            mkdir($path, 0700);
            $ext = pathinfo($_FILES['files']['name'][$f], PATHINFO_EXTENSION);
            $uniq_name = substr(uniqid(),-5) . '.' .$ext;
            $dest = $path . $uniq_name;

            if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $dest)){

           $qry = "INSERT INTO files (code, name, path, type) VALUES ('$dir','$uniq_name','$dest','$ext')" ;

            $result = mysqli_query($dbc, $qry);
            if ( false===$result ) {
            $sql_error .= 'Error in the query '.$qry.'  Error Desc :'.mysqli_error($dbc).'<br /><br />' ;
                }
            }

        }

    }       
}}

回答by Rikesh

Warning is quite clear, you are creating directory which already exists. So, just change it to

警告很清楚,您正在创建已经存在的目录。所以,只需将其更改为

if (!file_exists($path)) {
    mkdir($path, 0700);
}

回答by Birla

Use PHP's is_dir($path_to_dir)for checking if a directory exists from before.

使用 PHPis_dir($path_to_dir)检查之前的目录是否存在。

Official documentation

官方文档