【问题标题】:Check multiple levels of folders is dir检查多级文件夹是 dir
【发布时间】:2018-04-05 23:25:22
【问题描述】:

当用户输入一个新的文件夹名称,并且所有准备好的名称存在于子文件夹和主文件夹中时,将返回错误。

目前即使子文件夹等中不存在文件夹也会返回错误。

问题我怎样才能使它可以检查主目录和子文件夹并在退出时返回错误否则让我创建它。

文件夹名称的帖子是$this->input->post('folder')

检查是否存在同名的子文件夹。

if (!$json) {

    $results = scandir($DIR_IMAGE);

    foreach ($results as $result) {
        if ($result === '.' or $result === '..') continue;

        if (is_dir($DIR_IMAGE . '/' . $result)) {
            $json['error'] = 'This folder name all ready';
        }
    }
}

功能齐全

public function folder() {

    $json = array();

    $DIR_IMAGE = FCPATH . 'images/';

    if ($this->input->get('directory')) {
        $directory =  $this->input->get('directory') . '/';
    } else {
        $directory = 'catalog/';
    }

    if (!$json) {
        $arrPathParts = explode('/', $directory);

        if (count($arrPathParts) > 3) {
            $json['error'] = 'You can not create a new folder here try back one level.';
        }
    }

    if (!$json) {

        $re = '/\W/'; // \W matches any non-word character (equal to [^a-zA-Z0-9_])

        $str = $this->input->post('folder');

        $is_correct_foldername = !preg_match($re, $str) ? true : false; 

        if (!$is_correct_foldername) {
            $json['error'] = 'You have some symbols or no spaces that are not allowed';
        }
    }

    // Checks if any subfolders exists with same name.

    if (!$json) {

        $results = scandir($DIR_IMAGE);

        foreach ($results as $result) {
            if ($result === '.' or $result === '..') continue;

            if (is_dir($DIR_IMAGE . '/' . $result)) {
                $json['error'] = 'This folder name all ready';
            }
        }
    }

    // Every thing passes now will create folder
    if (!$json) {
        mkdir($DIR_IMAGE . $directory . $this->input->post('folder'), 0777, true);
        $json['success'] = 'Your folder is now created!';
    }

    $this->output->set_content_type('Content-Type: application/json');
    $this->output->set_output(json_encode($json));        
}

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    在您的代码中:

    if (!$json) {
    
        $results = scandir($DIR_IMAGE);
    
        foreach ($results as $result) {
            if ($result === '.' or $result === '..') continue;
    
            if (is_dir($DIR_IMAGE . '/' . $result)) {
                $json['error'] = 'This folder name all ready';
            }
        }
    }
    

    您只是检查从 scandir() 返回的每个目录名称是否是一个目录 - 始终是 true

    假设您要检查的目录名称是$this->input->post('foldername'),您可以这样做:

    if (!$json) {
    
        $results = scandir($DIR_IMAGE);
    
        foreach ($results as $result) {
            if ($result === '.' or $result === '..') continue;
    
            if (is_dir($DIR_IMAGE . '/' . $result) && ($result == $this->input->post('foldername'))) {
                $json['error'] = 'This folder name all ready';
            }
        }
    }
    

    附带说明,您应该尝试使用DIRECTORY_SEPARATOR 常量而不是'/' 目录。

    最后,还要注意目录名称的大小写可能是不同操作系统上的一个因素。

    【讨论】:

    • 我在哪里可以放置这个$this->input->post('foldername');,因为它会获取正在尝试创建的用户文件夹名称。
    • 我已经编辑了我的答案以指明它应该放在哪里。希望这会有所帮助!
    • 我发现使用 RecursiveTreeIterator 和 in_array 现在可以正常工作了。我为其他人发布了自己的答案。
    【解决方案2】:

    我现在有了一个使用 RecursiveIteratorIterator 和数组的解决方案

    public function folder() {
    
        $json = array();
    
        $DIR_IMAGE = FCPATH . 'images/';
    
        if ($this->input->get('directory')) {
            $directory =  $this->input->get('directory') . '/';
        } else {
            $directory = 'catalog/';
        }
    
        if (!$json) {
            $arrPathParts = explode('/', $directory);
    
            if (count($arrPathParts) > 3) {
                $json['error'] = 'You can not create a new folder here try back one level.';
            }
        }
    
        if (!$json) {
    
            $re = '/\W/'; // \W matches any non-word character (equal to [^a-zA-Z0-9_])
    
            $str = $this->input->post('folder');
    
            $is_correct_foldername = !preg_match($re, $str) ? true : false; 
    
            if (!$is_correct_foldername) {
                $json['error'] = 'You have some symbols or no spaces that are not allowed';
            }
        }
    
        // Checks if any subfolders exists with same name. @todo clean paths
    
        if (!$json) {
    
            $root = $DIR_IMAGE . 'catalog/';
    
            $iter = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
                RecursiveIteratorIterator::SELF_FIRST,
                RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
            );
    
            $paths = array($root);
    
            foreach ($iter as $path => $dir) {
                if ($dir->isDir()) {
                    $paths[] = basename($path);
                }
            }
    
            if (in_array($this->input->post('folder'), $paths)) {
                $json['error'] = 'You have all ready created a folder called ' . $this->input->post('folder') . ' in one of your directories!';
            }
        }
    
        if (!$json) {
            mkdir($DIR_IMAGE . $directory . $this->input->post('folder'), 0777, true);
            $json['success'] = 'Your folder is now created!';
        }
    
        $this->output->set_content_type('Content-Type: application/json');
        $this->output->set_output(json_encode($json));        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-08
      • 2011-02-12
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      相关资源
      最近更新 更多