簡體   English   中英

在 Codeigniter 中使用 FTP 上傳圖像

[英]Upload Image with FTP in Codeigniter

請幫助,我想將圖像插入數據庫並將圖像放入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>';
    }
}

在 Codeigniter 中使用 FTP 上傳圖像

我假設您正在嘗試在服務器上上傳圖像。

您遇到問題是因為您正在傳遞本地系統目錄的路徑$config['upload_path'] = 'C:/img/'; 服務器上不存在的。 所以把它改成類似的東西

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

其中img應該是您服務器上的文件夾。

注意:始終建議使用項目文件夾內的上傳路徑。 不是本地系統目錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM