0
votes

The following is a file upload feature built with codeigniter. This code works fine on locahost, but when I uploaded to the server, nothing works.

The folders are not created, and the data is not inserted into the database.

error_reporting(E_ERROR); // remove when error checking

class Upload extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        $this->load->model("upload_model");

        $this->config->load('uploads');
    }


    function index()
    {

    $upload_config = $this->upload_model->get_config();
            $data['upload_config']=$upload_config;
            $data['errors']='';
            $data['fb_user']=$fb_user;


            $this->form_validation->set_rules('userfile', 'UserFile', 'required');


            if ($this->input->post('submit'))
        {
            //Access bf_upload_settings table through Model function

            // Check and Create upload path
             if (!is_dir($upload_config['upload_path']))
                {
                    mkdir($upload_config['upload_path'], 0777, TRUE);

                }

            // Check and Create tmp directory
             if (!is_dir($upload_config['upload_path'].'/'.$this->config->item('tmp_dir')))
                {

                    if(!mkdir($upload_config['upload_path'].'/'.$this->config->item('tmp_dir')) )
                                    die('Failed to create folder: '.$this->config->item('tmp_dir') );
                    chmod($upload_config['upload_path'].'/'.$this->config->item('tmp_dir'),0777);
                }


            // Check and Create User directory (username used)


            // set the configurations fro  the  CI Upload class, using values from database in bf_upload_settings table
            $config['upload_path'] = $upload_config['upload_path'].'/'.$this->config->item('tmp_dir');
            $config['allowed_types'] = implode('|', unserialize( $upload_config['allowed_types'] ) );
            $config['max_size'] = $upload_config['max_size_kb']*1024; //Value stored in Database (KB) converted to Bytes



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

            if ( ! $this->upload->do_upload())
            {

                $data['errors']=$this->upload->display_errors();

            }   
            else
            {
                    $upload = $this->upload->data();

                    $data = array(
                      'name' => $upload['file_name'],
                      'file_size' => $upload['file_size'],
                      'file_path' => $upload['full_path'],
                      'file_type' => $upload['file_type'],
                      'user_id' => $user_profile['id']

                     );

                     $is_image=$upload['is_image']; // Checks if the uploaded file is an image file
                     $file_type=$upload['file_type'];
                     $name= $upload['file_name'];


                     //Check if file is a Video, if so, send it to the video folder in User Uploads Folder
                     if ($upload['file_type']=='video/mp4')
                     {
                         if (!is_dir($upload_config['upload_path'].'/'.$this->config->item('videos_dir')))
                            {
                                if(!mkdir($upload_config['upload_path'].'/'.$this->config->item('videos_dir')) )
                                    die('Failed to create folder: '.$this->config->item('videos_dir') );

                                chmod($upload_config['upload_path'].'/'.$this->config->item('videos_dir'),0777);
                            }   


                            copy($upload_config['upload_path'].'/'.$this->config->item('tmp_dir').'/'.$name, $upload_config['upload_path'].'/'.$this->config->item('videos_dir').'/'.$name);
                            // rename($upload_config['upload_path'].'/'.$this->config->item('videos_dir').'/'.$name, $upload_config['upload_path'].'/'.$this->config->item('videos_dir').$name);
                            unlink($upload_config['upload_path'].'/'.$this->config->item('tmp_dir').'/'.$name);

                     }
                     if ($upload['file_type']=='audio/mpeg' || 'audio/mp3')  //Check if file is an image, if so, send it to the images folder in User Uploads Folder
                     {
                        if (!is_dir($upload_config['upload_path'].'/'.$this->config->item('audio_dir')))
                            {
                                if(!mkdir($upload_config['upload_path'].'/'.$this->config->item('audio_dir')) )
                                    die('Failed to create folder: '.$this->config->item('audio_dir') ); 
                                chmod($upload_config['upload_path'].'/'.$this->config->item('audio_dir'),0777);

                            }   


                            copy($upload_config['upload_path'].'/'.$this->config->item('tmp_dir').'/'.$name, $upload_config['upload_path'].'/'.$this->config->item('audio_dir').'/'.$name);
                            // rename($upload_config['upload_path'].'/'.$this->config->item('audio_dir').'/'.$name, $upload_config['upload_path'].'/'.$this->config->item('audio_dir').$name);
                            unlink($upload_config['upload_path'].'/'.$this->config->item('tmp_dir').'/'.$name);
                     }


                    if ( $this->upload_model->insert($data) )
                    {

                        $this->load->view('upload_success');
                    }

                    else
                    {

                        redirect(current_url());

                    }


            }


        }

            $this->load->view('upload',$data);


    }
}

Nothing works now. How do I fix this?

UPDATE: Also, when I try to upload a file, in this case a .mp4 video, I get the error "The filetype you are attempting to upload is not allowed.", even though I have it set as an allowed type.

UPDATE 2:

I have been trying to upload videos (.mp4). Which I do have set as an allowed type. I tried uploading a pdf, and an image, and they both were uploaded successfully.

Why is the server not uploading the video file? How do I fix this?

1
check your $upload_config['upload_path'] path and permissions. Try to create $upload_config['upload_path'] manually and set 0777 permissions.Alex Kapustin
check your base_url path and upload_path alsoAgha Umair Ahmed
Still not working. Do I have to change any server configs. I am using filezillauser3237078
@AghaUmairAhmed My base_url is app.com/upload and my upload path is set to assets/uploadsuser3237078
given folder writing permission through cpanel or wat ever host account you havesaurabh kamble

1 Answers

0
votes

it is your document root path not base url you have to define in constants.php like this

define('UPLOAD_PATH',   $_SERVER['DOCUMENT_ROOT'].'/yoururl/assets/uploads/');

and when echo use some thing like that

UPLOAD_PATH.yourimg