Codeigniter -> File Upload Path | Folder Create

16,133

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 :

  1. i have added the permission to the folder so that you can upload files to it.
  2. 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
}
Share:
16,133
Jess McKenzie
Author by

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, 2022

Comments

  • Jess McKenzie
    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
    Jess McKenzie about 12 years
    I want to create that $folderName on the server
  • Jess McKenzie
    Jess McKenzie about 12 years
    When 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
    Jess McKenzie about 12 years
    Works without the if is that ok?
  • Zaher
    Zaher about 12 years
    the 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
    Mischa about 12 years
    You 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
    Mischa about 12 years
    You can also you file_exists to check if a directory exists.
  • Zaher
    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
    Mischa about 12 years
    What does readability have to do with it? With the else the code does not work the way it is supposed to work.
  • Zaher
    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 ..