Codeigniter make directory if not exist
Asked Answered
B

3

5

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.

Bronchiectasis answered 8/5, 2013 at 8:5 Comment(6)
":" colon character is not allowed in directory name.Rescue
try to give complete path. and enclosed in in double quotes.Wowser
@elavarasanlee thanks for that info I'll try it now, and will get back to you if it fixBronchiectasis
Use date('Y-m-d H.i.s') instead of ':'.Rescue
I have tried $date = 'abc' line two of above code, to test if date thing is my problem here, but still i got the same errorBronchiectasis
wait exclude ":" works, sorry for that. thanks guys!Bronchiectasis
C
19

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')
Consummation answered 8/5, 2013 at 8:8 Comment(1)
@kodewrecker, no problem. Glad i could help. Don't forget that you must change the date BEFORE you set it up in the codeigniter config. Or else that wil fail still ;)Consummation
W
5
if(!is_dir($config['upload_path'])) mkdir($config['upload_path'], 0777, TRUE);
Wenger answered 15/4, 2014 at 15:46 Comment(2)
This answer is in the Low Quality Posts review queue because it's just a line of code with no explanation. Please explain what your code does and how it answers the question.Thracophrygian
Good answer doesn't need explanation. This is the best answer.Sarawak
B
0

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.

Blindfold answered 17/10, 2019 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.