Jess McKenzie
Jess McKenzie

Reputation: 8385

Codeigniter -> File Upload Path | Folder Create

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; 
}

Upvotes: 4

Views: 16659

Answers (4)

Zaher
Zaher

Reputation: 1150

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 )..

Upvotes: 8

Mischa
Mischa

Reputation: 43298

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; 

Upvotes: 2

hohner
hohner

Reputation: 11588

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
}

Upvotes: 1

peacemaker
peacemaker

Reputation: 2591

Not 100% sure what you want from your description so here are a few suggestions:

If you need to programmatically create a folder using PHP you can use the mkdir() function, see: http://php.net/manual/en/function.mkdir.php

Also, check out the Codeignitor file helper for reading and writing files: http://codeigniter.com/user_guide/helpers/file_helper.html

If the folder already exists, you need to make sure the permissions are write-able. On windows you can right click on the folder, properties, security and edit there. On Linux you'll want the chmod command.

Upvotes: 0

Related Questions