在Codeigniter中使用FTP上传图像

时间:2016-04-05 12:45:22

标签: php codeigniter ftp

请帮助,我想将图像插入数据库并将图像放在ftp中,只保存图像路径。但是,它始终显示“上传路径似乎无效”。请帮我解决我的代码有什么问题?请帮助.. 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>';
    }
}

1 个答案:

答案 0 :(得分:1)

  

在Codeigniter中使用FTP上传图片

我假设您正在尝试在服务器上上传图片。

您遇到了问题,因为您正在传递服务器上不存在的本地系统目录$config['upload_path'] = 'C:/img/';的路径。所以把它改成像

这样的东西
 $config['upload_path'] = APPPATH . 'img';

img应该是服务器上的文件夹。

注意:始终建议在项目文件夹中使用上传路径。而不是本地系统目录