3

I'm trying to recursively create a directory using php's mkdir function in a Codeigniter installation. My code looks like this:

mkdir('docs/client/bills/payd', 0777, true)

The docs directory already exists in my site root dir, the client directory is beeing created with 0755 permission, the bills directory is beeing created with permission 1341 (weird!) and the last directory, payd, is never created.
I tryed to change permission in the mkdir argument list to 0, 755, etc... and nothing has changed. I also tryed to set umask to 0, 0777... and nothing.

umask(0777);
mkdir('docs/client/bills/payd', 0777, true)

Can anyone please say what am I doing wrong? The code above is called from a Codeigniter regular controller.

2
  • After try both responses below from Lorenzo Magno and Tpojka I realized that cPanel was creating the same path with the same errors. Then I perceived yet that last_modified time of directories creation was the same time when a had created the directories yesterday... cPanel was recovering my wrong version of directory creation. I just changed the client name and creation worked correctly. The problem started because I had not set the recursive argument to true in my first try and when I done that cPanel kept recreating/recovering the first state of error. Commented Aug 25, 2016 at 11:59
  • In conclusion, my first version of code works so works the responses from Lorenzo and Tpojka... I accepted the Lorenzo's answer as the correct one just because he answered first. Thank you to both fellows. Commented Aug 25, 2016 at 12:11

3 Answers 3

1

Try with

if ( ! is_dir( FCPATH.'docs/client/bills/payd' )//FCPATH is absolute path to the project directory
{
    mkdir( FCPATH.'docs/client/bills/payd', 0777, true );//although 0755 is just fine and recomended for uploading and reading
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use this to specify it the working directory, it might be confused as to where the directory is located.

mkdir( getcwd().'docs/client/bills/payd', 0777, true);

getcwd is the working directory for your codeigniter. You can search in the PHP guide the getcwd() function to make it clearer.

This should work.

EDIT

To make it clearer, that would return the following:

C:\xampp\htdocs\YOUR_ROOT_DIRECTORY\docs\client\bills\payd

EDIT AGAIN

Although after reading again, this would only create payd and assume that docs\client\bills is already created. You could create client and bills using mkdir or using the file explorer. But there are other PHP ways and I can help if needed.

Goodluck meyt

Comments

0

I also had this weird "1341" permissions error with PHP mkdir, nothing to do with CodeIgniter, it's a pure PHP issue!

After much experimentation, the only way I could get it to work was to include a slash at the end of the path, and set the recursive flag to 'true'. (Even though the PHP docs don't show a final slash, and I was only creating a single directory.)

I.e.

$existing_path = '/these/directories/already/exist/';
mkdir( $existing_path . 'new-directory/', 0755, true);

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.