Codeigniter -> File Upload Path | Folder Create
Solution 1
am not sure what they are talking about by using the file_exist function since you need to check if its the directory ..
$folderName = $this->model->functionName()->tableName;
$config['upload_path'] = "this/$folderName/";
if(!is_dir($folderName))
{
mkdir($folderName,0777);
}
please note that :
- i have added the permission to the folder so that you can upload files to it.
- i have removed the
else
since its not useful here ( as @mischa noted )..
Solution 2
This is not correct:
if(!file_exists($folderName))
{
mkdir($folderName);
}
else
{
// Carry on with upload
}
Nothing will be uploaded if the folder does not exist! You have to get rid of the else clause.
$path = "this/$folderName/";
if(!file_exists($path))
{
mkdir($path);
}
// Carry on with upload
$config['upload_path'] = $path;
Solution 3
Not quite sure what you mean by 'dictionary', but if you're asking about how to create a variable:
$folderName = $this->model->functionName()->tableName;
$config['upload_path'] = "this/$folderName/";
To add/check the existence of a directory:
if(!file_exists($folderName))
{
mkdir($folderName);
}
else
{
// Carry on with upload
}
Jess McKenzie
Feel bad about having so many posts on here but the only way you get to learn is by taking it in and asking questions.
Updated on June 27, 2022Comments
-
Jess McKenzie almost 2 years
Currently I have the following:
$config['upload_path'] = 'this/path/location/';
What I would like to do is create the following controller but I am not sure how to attack it!!
$data['folderName'] = $this->model->functionName()->tableName; $config['upload_path'] = 'this/'.$folderName.'/';
How would I create the
$folderName?
dictionary on the sever?Jamie:
Could I do the following?
if(!file_exists($folderName)) { $folder = mkdir('/location/'$folderName); return $folder; } else { $config['upload_path'] = $folder; }
-
Jess McKenzie about 12 yearsI want to create that $folderName on the server
-
Jess McKenzie about 12 yearsWhen I upload the image I want it to be uploaded into the folder $folderName but this folder does not exists I need to create it. Possibly doing this before the upload function
-
Jess McKenzie about 12 yearsWorks without the if is that ok?
-
Zaher about 12 yearsthe if will check the to see if the folder is exists or not , and if its exist then it will not create it nor throw any warning or so..
-
Mischa about 12 yearsYou don't need the else clause! If the folder doesn't exit you still want to carry on with upload (after creating the folder).
-
Mischa about 12 yearsYou can also you
file_exists
to check if a directory exists. -
Zaher about 12 years@mischa your right in both .. but from my point of view its better to make your code readable for every one ..
-
Mischa about 12 yearsWhat does readability have to do with it? With the
else
the code does not work the way it is supposed to work. -
Zaher about 12 years@mischa i got your point .. the
else
is not useful here unless he wanted to do something after he knows that the folder is exist .. i will edit it right a way ..