-2

Which file permission should I use for uploading and downloading files to a folder in codeigniter?

My whole project is been hosted on FileZilla.

NOTE: Uploading downloading works perfectly fine when website is hosted loacally.

Error only occurs when hosted through FileZilla. My Upload/Download folder is present in root directory of codeigniter (Directory where Application folder is present).

Upload Controller Code

public function do_upload(){  
$rti_details['rtino'] = $this->input->post("rtino");

    $rtino_result = $this->rti_model->get_rti_details_by_rtino($rti_details['rtino']);
    if(!$rtino_result){ 

        $upload=$this->upload_file('rtifile',$this->input->post('rtino')); 
            if($upload)
            {
                $data = array('rti_no'=>$this->input->post('rtino'),
                          'filer_name'=>$this->input->post('filername'),
                          'filer_add'=>$this->input->post('fileradd'), 
                          'city'=>$this->input->post('city'),
                          'state'=>$this->input->post('state'),
                          'pin_code'=>$this->input->post('pin_code'),
                          'rti_cat'=>$this->input->post('rti_cat'),
                          'rti_file'=>$upload['full_path'],
                          'filed_on'=>$this->input->post('filedon')
                          );

            $this->rti_model->insert_rti($data);
            }

        $result = true;
    }
    else
        $result = false;

    if($result)
        $this->session->set_flashdata("flashSuccess","RTI added successfully");
    else
        $this->session->set_flashdata("flashError","Error in adding RTI. This RTI Number Already Exist.");

    redirect("rti/rti_file");

}

private function upload_file($name ='',$sno = 0)
{
    if($name=='rtifile'){   $config['upload_path'] = 'assets/rti_uploads/rti_file/';    }
    if($name=='coverletter'){   $config['upload_path'] = 'assets/rti_uploads/cover_letter/';    }
    if($name=='fullreply'){ $config['upload_path'] = 'assets/rti_uploads/full_reply/';  }
    $config['allowed_types'] = 'pdf';
    $config['max_size']  = '2050';


        if(isset($_FILES[$name]['name']))
        {
            if($_FILES[$name]['name'] == "")
                $filename = "";
            else
            {
                $filename=$this->security->sanitize_filename(strtolower($_FILES[$name]['name']));
                $ext =  strrchr( $filename, '.' ); 
                if($name=='rtifile'){   $filename='RTI_'.$sno.'_'.date('YmdHis').$ext;  }
                if($name=='coverletter'){   $filename='COVER_'.$sno.'_'.date('YmdHis').$ext;    }
                if($name=='fullreply'){ $filename='FULLREPLY_'.$sno.'_'.date('YmdHis').$ext;    }
            }
        }
        else
        {
            $this->session->set_flashdata('flashError','ERROR: File Name not set.');
            redirect('rti/rti_file');
            return FALSE;
        }

        $config['file_name'] = $filename;

        if(!is_dir($config['upload_path'])) 
        {
            mkdir($config['upload_path'],0777,TRUE);
        }

        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        if ( ! $this->upload->do_upload($name)) 
        {
            $this->session->set_flashdata('flashError',$this->upload->display_errors('',''));
            redirect('rti/rti_file');
            return FALSE;
        }
        else
        {
            $upload_data = $this->upload->data();
            return $upload_data;
        }
}
hellokuldeep
  • 25
  • 2
  • 10
  • For public directories `755` or `0755` for public files `644` or `0644`. Check [this](https://forums.cpanel.net/threads/why-are-644-and-755-unix-permissions-ideal-for-files-directories-in-public-folders.136821/) topic. – Tpojka Sep 03 '16 at 17:24
  • Please add you upload controller code to your question it bit hard to guess. Also have you followed https://codeigniter.com/userguide3/general/styleguide.html#file-naming –  Sep 04 '16 at 02:04

1 Answers1

2

From the documentation:

You’ll need a destination directory for your uploaded images. Create a directory at the root of your CodeIgniter installation called uploads and set its file permissions to 777.

http://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-upload-directory

Best wishes,

Paul

PaulD
  • 1,161
  • 1
  • 9
  • 14
  • I already have directory at root of my CodeIgniter installation, where files are being normally uploaded and downloaded only when website is hosted locally. But when I host this on FileZilla the above error occurs. – hellokuldeep Sep 03 '16 at 18:15
  • Also 777 is not secure to use. – hellokuldeep Sep 03 '16 at 18:24
  • Well you will have to give more details. You say "The Error" but what error? You never said in your OP. You implied it was a permissions error so you start with 777 and test. If it works go to 755. And yes, you always use the lowest possible permissions in production. Where your App folder is, is not necessarily your root, in fact in production you should move your App directory above the public access. – PaulD Sep 03 '16 at 18:57