0

I'm using PHP 5 and Codeigniter 3 on Ubuntu Linux.

I need to upload and store user files on the server, but before upload I need to create directory for user where files will be stored. Directory name is based on userdata hashed with md5.

Also Im checking if directory is already created/exists on server, otherwise should be created.

   public function uploadImage(){

        $id = $this->session->userdata('id');

        $dir_name = md5($id); // create hashed dir name 

        $user_dir = base_url() . 'uploads/user' . '/' . $dir_name;

       // print_r($user_dir);

        if(!file_exists($user_dir)){
            mkdir($user_dir, 0777);
            print_r("created");
        }else{
            print_r("directory exists");
        }
 }

When I run code, it says created, but I cant see directory. It does not exist in uploads/users folder. Please help me, where Im wrong. Many thanks

djuro djuric
  • 39
  • 1
  • 1
  • 6
  • Possible duplicate of https://stackoverflow.com/questions/6449386/base-url-function-not-working-in-codeigniter – pokeybit Jun 08 '17 at 17:42
  • It could be that you are trying to create a folder inside of a folder where apache doesn't have write access. The print_r statement will be reached even if the mkdir wasn't succesful and is non-indicative of anything. – Rimble Jun 08 '17 at 17:43
  • You are printed "created" if the directory doesn't exist. This is not the same as printing "created" upon a successful creation. `var_dump(mkdir(User_dir, 0777));` and I bet you get false. – Matt Jun 08 '17 at 17:49
  • Rimble I guess it is not problem. I have pemissions to write in these directories. – djuro djuric Jun 08 '17 at 17:49
  • Yes it returns false mkaatman – djuro djuric Jun 08 '17 at 17:51
  • 2 cases that i know of ,1st one if the directory already exist, 2nd one your current working directory (mine was html) has no write assess(was 775). I did the following and was working , `cd /var/www/ && chmod 777 html` – Jayakrishnan Jun 08 '17 at 19:19

1 Answers1

0

USE FCPATH

public function uploadImage(){

   $id = $this->session->userdata('id');

   $dir_name = md5($id); // create hashed dir name 

   /*
       You may need / after FCPATH 

       $user_dir = FCPATH . '/uploads/user/' . $dir_name;
   */

   $user_dir = FCPATH . 'uploads/user/' . $dir_name;

   // print_r($user_dir);

    if(!file_exists($user_dir)){

        mkdir($user_dir, 0777);

        // @mkdir($user_dir, 0777, true);

        print_r("created");

    } else {

        print_r("directory exists");
    }
}