简体   繁体   中英

Upload Image with FTP in Codeigniter

Please Help, I want to insert image to database and put the image in the ftp and only save the image path. But, it always show "The upload path does not appear to be valid." Please help me what's wrong with my code? Please Help.. Thx

function insert_barang() {
    $this->load->library('form_validation');

    $data['base_url'] = $this->config->item('base_url');

    $this->form_validation->set_rules('nama', 'nama', 'required|max_length[100]');
    $this->form_validation->set_rules('desk', 'desk', 'required|max_length[100]');

    if($this->form_validation->run() == FALSE) {
         $this->load->view('tambah_barang_view',array('error' => ''));
         echo '<script>alert("Insert Barang Failed");</script>';
    }

    $config['upload_path'] = 'C:/img/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width'] = '2592';
    $config['max_height'] = '1456';

    $this->load->library('upload', $config);
    if(!$this->upload->do_upload('userfile')) {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('tambah_barang_view',$error);
        echo '<script>alert("Data Gagal di Upload");</script>';
    } else {
        $data['id_penjual'] = $this->session->userdata['id_penjual'];
        $data['nama'] = $this->input->post('nama');
        $data['harga'] = $this->input->post('harga');
        $data['stok'] = $this->input->post('stok');
        $data['desk'] = $this->input->post('desk');
        $data['id_kat'] = $this->input->post('kategori');
        $data['jenis_hewan'] =$this->input->post('jenis_hewan');

        $upload_data =  $this->upload->data();
        $filename= $upload_data['file_name'];
        $source = 'C:/img/'.$fileName;

        $this->load->library('ftp');
        //FTP configuration
        $ftp_config['hostname'] = 'ftp.lomapod.esy.es';
        $ftp_config['username'] = '******';
        $ftp_config['password'] = '******';
        $ftp_config['debug']    = TRUE;

        //Connect to the remote server
        $this->ftp->connect($ftp_config);

        //File upload path of remote server
        $destination = '/public_html/assets/'.$fileName;

        //Upload file to the remote server
        $this->ftp->upload($source, ".".$destination);

        //Close FTP connection
        $this->ftp->close();
        @unlink($source);

        $data['imgProfile'] = $upload_data['full_path'];

        $data['base_url'] = $this->config->item('base_url');

        $this->load->model('Seller_model');

        $data['last_id'] = $this->Seller_model->InsertImage($data['imgProfile']);
        $this->seller_model->InsertBarang($data['nama'], $data['harga'],$data['stok'],$data['desk'],$data['id_kat'],$data['id_penjual'],$data['last_id'],$data['jenis_hewan']);

        $this->load->view('home_view');
        echo '<script>alert("Your form was successfully submitted!");</script>';
    }
}

Upload Image with FTP in Codeigniter

I am assuming with the above line that you are trying to upload image on server.

You are getting the issue because you are passing the path of your local system directory $config['upload_path'] = 'C:/img/'; which is not exist on server. So change it to something like

 $config['upload_path'] = APPPATH . 'img';

Where img should be a folder on your server.

NOTE : it is always recommended to use upload path inside of your project folder. not to a local system directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM