Search code examples
phpjavascriptuploadify

Unable to create folder


Am using uploadify to allow users on my site upload profile pictures which stored in folders corresponding to their username. For example, a user with username "mrx" has folder "/beta/profiles/mrx/avatars/". It works well on my local system, but when i uploaded only the "mrx" parent folder is created with the permission "0000"; I pass the upload folder for the users via a hidden input which has the path to their folder as its value. Am using codeigniter and webroot looks like this

public_html/ beta/ application/ system/ assets/ profiles/ /mrx/ m/

The mrx folder is supposed to have the "avatars" inside it,but is not created by my uploadify script;

This is the uploadifiy script

if (!empty($_FILES)) {
   $file = new Uploader();
   $path = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
   if(!file_exists($path))
   {
      mkdir($path,0777,true);
   }
   $file_temp = $_FILES['Filedata']['tmp_name'];
   $file_name = $file->prep_filename($_FILES['Filedata']['name']);
   $file_name = ereg_replace(" ","_",$file_name);

   $file_ext = $file->get_extension($_FILES['Filedata']['name']);
   $real_name = $file_name;
   $newf_name = $file->set_filename($path, $file_name, $file_ext,TRUE);
   $file_size = round($_FILES['Filedata']['size']/1024, 2);
   $file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES['Filedata']['type']);
   $file_type = strtolower($file_type);
   $targetFile =  str_replace('//','/',$path) . $newf_name;
   move_uploaded_file($file_temp,$targetFile);

   $filearray = array();
   $filearray['file_name'] = $newf_name;
   $filearray['real_name'] = $real_name;
   $filearray['file_ext'] = $file_ext;
   $filearray['file_size'] = $file_size;
   $filearray['file_path'] = $targetFile;
   $filearray['file_temp'] = $file_temp;
   //$filearray['client_id'] = $client_id;

   $json_array = json_encode($filearray);
   echo $json_array;
}else{
    echo "1";   
}

And this the controller that passes the folder to uploadify

function avatar()
        {

            $this->isloggedin();
            $data['title'] = "Your Avatar | PheedBakk";
            $data['username'] = $this->session->userdata("username");
            $data['user_id'] = $this->session->userdata('user_id');
            $data['avatar'] = $this->site_config->get_setting_value('default_avatar');
            $data['user'] = $this->user_model->profile($data['username']);
            $data['upload_folder'] = "/beta/profiles/".$this->session->userdata('username')."/avatars";



            $this->load->view('templates/user_header',$data);
            $this->load->view('templates/user-left-bar');
            $this->load->view('settings/avatar');
            $this->load->view('templates/user-right-bar');
            $this->load->view('templates/footer');
        }

What am doing wrong?


Solution

  • Create one folder and then the other without recursive set to true...

    $path1 = "/beta/profiles/$username";
    $path2 =  "/beta/profiles/$username/avatars";
    
        mkdir($path1,0777);
        mkdir($path2,0777);
    

    Or call chmod on the directory once the folder is created:

    chmod("/beta/profiles/$username/avatars", 0777);