php Codeigniter 生成目录(如果不存在)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16435597/
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
Codeigniter make directory if not exist
提问by CaffeineShots
Hi can anyone help me with this. Basically I used the file uploading class of codeigniter the link is
hereand it works fine! but I need to know how to create directory inside this default upload path $config['upload_path'] = './uploads/'
the name of the folder is the date today this is my sample code.
嗨,任何人都可以帮我解决这个问题。基本上我使用了codeigniter的文件上传类链接在
这里,它工作正常!但我需要知道如何在这个默认上传路径中创建目录$config['upload_path'] = './uploads/'
文件夹的名称是今天的日期这是我的示例代码。
date_default_timezone_set('Asia/Manila');
$date = date('Y-m-d H:i:s');
$config['upload_path'] = './uploads/'.$date;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
if (!is_dir('uploads/'.$date)) {
mkdir('./uploads/' . $date, 0777, TRUE);
}
and I got an error like
我得到了一个错误
Message: mkdir(): Invalid argument
I search how it will be fix and they say this 0777
CI permission will do, but still it won't work for me.
我搜索它将如何修复,他们说这个0777
CI 权限可以,但它仍然对我不起作用。
Thanks for any response.
感谢您的任何回应。
回答by Mathlight
The date format wil have specail chars like -
and :
日期格式将具有特殊字符,例如-
和:
I'm not sure about the -
but :
is not allowed on an map. So you have to delete that char out $date
.
我不确定-
但:
不允许出现在地图上。所以你必须删除那个字符$date
。
$date = str_replace( ':', '', $date);
if (!is_dir('uploads/'.$date)) {
mkdir('./uploads/' . $date, 0777, TRUE);
}
or use as elavarasan leesaid use:
或使用elavarasan lee所说的使用:
date('Y-m-d H.i.s')
回答by Paulo
if(!is_dir($config['upload_path'])) mkdir($config['upload_path'], 0777, TRUE);
回答by Dinesh Gurjar
if you are new to codeigniter and previously in php you used code for making directory like
如果您不熟悉 codeigniter 并且以前在 php 中使用过代码来制作目录,例如
$target_dir="../../media/profile/".date('my')."/";
if(!file_exists($target_dir)){
mkdir($target_dir,0777);
}
then in codeignitor you can use only one dot for outside directory path
然后在 codeignitor 中,您只能使用一个点作为外部目录路径
$target_dir="././media/profile/".date('my')."/";
if(!file_exists($target_dir)){
mkdir($target_dir,0777);
}
this one solved my problem.
这个解决了我的问题。