2016-04-05 62 views
2

请帮助,我想插入图像到数据库并将图像放在ftp中,只保存图像路径。但是,它总是显示“上传路径似乎不正确。”请帮我解决我的代码有什么问题?请帮助.. THX使用FTP在Codeigniter上传图像

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

任何文件夹都存在于'C:'驱动器中,如'img' –

+0

总是建议在项目文件夹中使用上传路径。不到本地系统目录 – urfusion

回答

1

与FTP上传图片的笨

我跟你试图上传图像服务器上面的行假设。

由于您传递的服务器上不存在本地系统目录$config['upload_path'] = 'C:/img/';的路径,您将会遇到问题。因此将其改为类似于

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

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

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

+0

什么是appath? 我应该将appath设置为? ,因为它仍然显示“上传路径似乎不是有效的。” –

+0

@NickyApriliani:APPPATH是你的应用程序文件夹 – urfusion

+0

我该怎么处理这段代码“$ source ='C:/ img /'.$ fileName; ”我该怎么办? –