4

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; 
}
3
  • I'm not following - Wouldn't you already have access to that value via $data['folderName']? Commented Apr 9, 2012 at 20:13
  • Yes Colin but I need to somehow incorporate mkdir or similar to create the folder on the server. Commented Apr 9, 2012 at 20:14
  • You might want to clarify that you're asking how to create a directory named $data['folderName'] - I interpreted your question differently. Commented Apr 9, 2012 at 20:15

4 Answers 4

8

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 )..
Sign up to request clarification or add additional context in comments.

7 Comments

Works without the if is that ok?
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..
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).
You can also you file_exists to check if a directory exists.
@mischa your right in both .. but from my point of view its better to make your code readable for every one ..
|
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; 

Comments

1

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
}

2 Comments

I want to create that $folderName on the server
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
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.