【发布时间】: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