【问题标题】:Upload Image with FTP in Codeigniter在 Codeigniter 中使用 FTP 上传图像
【发布时间】:2016-04-05 12:45:22
【问题描述】:

请帮助,我想将图像插入数据库并将图像放入 ft​​p 并仅保存图像路径。但是,它总是显示“上传路径似乎无效”。请帮我看看我的代码有什么问题?请帮忙..谢谢

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>';
    }
}

【问题讨论】:

  • C: 驱动器中存在任何文件夹,例如 img
  • 始终建议使用项目文件夹内的上传路径。不要到本地系统目录

标签: php codeigniter ftp


【解决方案1】:

在 Codeigniter 中使用 FTP 上传图片

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

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

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

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

注意:始终建议使用项目文件夹内的上传路径。不要到本地系统目录

【讨论】:

  • 什么是appath?我应该将appath设置为吗?因为它仍然显示“上传路径似乎无效。”
  • @NickyApriliani : APPPATH 是您的应用程序文件夹
  • 我应该如何处理这段代码 "$source = 'C:/img/'.$fileName; "呢?
  • $source = $config['upload_path']."/".$fileName;
  • 问题已经解决了,但是现在出现这个错误。“无法上传指定的文件。请检查你的路径。”如何解决?
最近更新 更多