简体   繁体   English

在 Codeigniter 中使用 FTP 上传图像

[英]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.请帮助,我想将图像插入数据库并将图像放入ftp并仅保存图像路径。 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请帮助.. 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在 Codeigniter 中使用 FTP 上传图像

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/';您遇到问题是因为您正在传递本地系统目录的路径$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.其中img应该是您服务器上的文件夹。

NOTE : it is always recommended to use upload path inside of your project folder.注意:始终建议使用项目文件夹内的上传路径。 not to a local system directory不是本地系统目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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