我想知道是否有可能阻止人们创建4深的子文件夹
询问如何使它成为当用户想要创建一个4深的子文件夹时,它不会让该用户。我只想要它,所以可以创建3个深子文件夹。
$this->input->get('directory')目录/home/view示例
public function folder()
{
$json = array();
if ($this->input->get('directory') != '') {
$directory = FCPATH . 'images/'. $this->input->get('directory') . '/';
@mkdir($directory . $this->input->post('folder'), 0777, true);
$json['success'] = true;
} else {
$directory = FCPATH . 'images/catalog/';
@mkdir($directory . $this->input->post('folder'), 0777, true);
$json['success'] = true;
}
$this->output->set_content_type('Content-Type: application/json');
$this->output->set_output(json_encode($json));
}发布于 2018-04-01 08:42:30
我首先想到的是
为什么允许用户创建文件夹? 如果有人知道你在这里做什么,并且理解这个过程--你很可能会被淹没(突然,你这里有成千上万的文件夹.)
但是如果这不是你所关心的,你在这里所面临的是一个非常困难的问题,这个问题一点也不小,因为你需要做很多检查。
我可以给你一个开始-但要知道这并不是全部.如果您真的将其设置为生产状态,那么唯一的调试方法就是结果;)
检查您得到的目录是否有效。为此,我从here获取了函数,删除了/并添加了一个. (在本例中,它将防止任何人走出根目录)
下面的代码是一个片段,但它应该清楚我的意图,可以很容易地扩展.
try
{
$strBaseDirectory = FCPATH . 'images/';
$strUserDirectory = $this->input->get('directory');
if (empty($strUserDirectory) || strpbrk($strUserDirectory, "\\.?%*:|\"<>")) throw new InvalidArgumentException('Invalid directory');
$strUserDirectory = trim($strUserDirectory, '/');
$arrPathParts = explode('/', $strUserDirectory);
if (count($arrPathParts) > 3) throw new InvalidArgumentException('To much folders...');
$arrPathParts = array_map(function($item) {
return trim($item);
}, $arrPathParts);
$key = array_search('', $arrPathParts);
if ($key) throw new InvalidArgumentException('One or more Foldername(s) contain only spaces...');
echo '<pre>';
print_r($arrPathParts);
$strUserDirectory = implode('/', $arrPathParts);
$blnDirectoryCreated = mkdir($strBaseDirectory.$strUserDirectory);
$arrJson = [
'success' => $blnDirectoryCreated,
'message' => 'whatever...'
];
}
catch (Exception $e)
{
$arrJson = [
'success' => false,
'message' => $e->getMessage()
];
}
$this->output->set_content_type('Content-Type: application/json');
$this->output->set_output(json_encode($arrJson));https://stackoverflow.com/questions/49595828
复制相似问题