Codeigniter make directory if not exist

陌路散爱 提交于 2020-04-10 03:27:33

问题


Hi can anyone help me with this. Basically I used the file uploading class of codeigniter the link is here and 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.

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.

Thanks for any response.


回答1:


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 = str_replace( ':', '', $date);
if (!is_dir('uploads/'.$date)) {
    mkdir('./uploads/' . $date, 0777, TRUE);

}

or use as elavarasan lee said use:

date('Y-m-d H.i.s')



回答2:


if(!is_dir($config['upload_path'])) mkdir($config['upload_path'], 0777, TRUE);



回答3:


if you are new to codeigniter and previously in php you used code for making directory like

$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

$target_dir="././media/profile/".date('my')."/";
    if(!file_exists($target_dir)){
        mkdir($target_dir,0777);
    }

this one solved my problem.



来源:https://stackoverflow.com/questions/16435597/codeigniter-make-directory-if-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!