当用户键入新文件夹名称并且所有名称都已存在于子文件夹和主文件夹中时,则会返回错误。
目前,即使子文件夹等中不存在文件夹,也会返回错误。
问题如何才能检查主目录和子文件夹并返回错误,如果退出,则允许我创建它。
foldername的帖子是$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));
}
答案 0 :(得分: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
常量而不是'/'
目录。
最后,还要注意目录名称大写可能是不同操作系统的一个因素。
答案 1 :(得分:0)
我现在有一个使用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));
}