2016-12-27 55 views
0

我試着上傳使用笨和AJAX文件,但我的形式始終顯示錯誤:上傳圖像文件總是拋出upload_path錯誤

the upload path isn't correct.

型號

public function save($data) 
    { 
     $this->db->insert($this->table, $data); 
     return $this->db->insert_id(); 
    } 

控制器

public function ajax_add(){ 

     $this->_validate(); 

     $data = array(

       'nama'=>$this->input->post('post_nama'), 
       'jenis_kelamin'=>$this->input->post('post_jk'), 
       'alamat'=>$this->input->post('post_alamat'), 
       'email'=>$this->input->post('post_email'), 
       'telepon'=>$this->input->post('post_telepon'), 
       'status'=>$this->input->post('post_status') 
      ); 

      if(!empty($_FILES['photo']['name'])) 
      { 
       $upload = $this->_do_upload(); 
       $data['photo'] = $upload; 
      } 

      $insert = $this->post->save($data); 

      echo json_encode(array("status" => TRUE)); 
    } 

private function _do_upload() 
    { 
     $config['upload_path'] = './upload/profil/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = 1000; //set max size allowed in Kilobyte 
     $config['max_width'] = 3000; // set max width image allowed 
     $config['max_height'] = 1500; // set max height allowed 
     $config['file_name'] = round(microtime(true) * 1000); 

     $this->load->library('upload', $config); 

     if(!$this->upload->do_upload('photo')) //upload and validate 
     { 
      $data['inputerror'][] = 'photo'; 
      $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error 
      $data['status'] = FALSE; 
      echo json_encode($data); 
      exit(); 
     } 
     return $this->upload->data('file_name'); 
    } 

查看

<form action="#" id="form" method="post" enctype="multipart/form-data"> 
<div class="form-group"> 
     <label class="control-label">Foto</label> 
     <input name="photo" type="file" id="photo"> 
     <span class="help-block"></span> 
</div> 
    <div class="form-group"> 
     <button type="button" id="btnSave" onclick="insert()" class="btn btn-success"> 
      <span class="glyphicon glyphicon-floppy-disk"></span> Simpan 
     </button> 
     <button type="reset" class="btn btn-default"> 
      <span class="glyphicon glyphicon-floppy-disk"></span> Clear 
     </button> 
    </div> 

阿賈克斯

url = "<?php echo site_url('profil/ajax_add')?>"; 
// ajax adding data to database 
var formData = new FormData($('#form')[0]); 
$.ajax({ 
    url : url, 
    type: "POST", 
    data: formData, 
    contentType: false, 
    processData: false, 
    dataType: "JSON", 
    success: function(data) 
    { 

     if(data.status) //if success close modal and reload ajax table 
     { 
      alert('Data Berhasil disimpan'); 
      reset_form(); 

     } 
     else 
     { 
      for (var i = 0; i < data.inputerror.length; i++) 
      { 
       $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class 
       $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string 
      } 
     } 
     $('#btnSave').text('save'); //change button text 
     $('#btnSave').attr('disabled',false); //set button enable 


    }, 
    error: function (jqXHR, textStatus, errorThrown) 
    { 
     alert('Error adding/update data'); 
     $('#btnSave').text('save'); //change button text 
     $('#btnSave').attr('disabled',false); //set button enable 

    } 
}); 
} 

當我點擊我的形式保存按鈕,表單總是顯示此錯誤:

Upload error: The upload path does not appear to be valid.

+0

是您在公共服務器,如「godaddy」或本地主機? – Vickel

+0

使用「./ upload/profile」煽動的「upload/profile」。 –

+0

如果您使用ajax調用上傳圖像,可以使用jquery.form.js插件通過ajax將圖像上載到服務器。 http://malsup.com/jquery –

回答

1

公共服務器上託管(例如共享主機提供商),您需要提供最有可能的上傳文件夾的完整相對路徑,這與本地主機環境不同。另外,還要確保你擁有所有權限,文件寫入該目錄

你可以在本地主機環境中使用

$config['upload_path'] = './upload/profil'; 

而是一個共享的主機,你需要得到更具體的,正常像

$config['upload_path'] = '/home/yourserver/public_html/upload/profil'; 

你可以找到這個upload_path,如在您的帳戶的cPanel在左欄的主網頁或可能想打電話給你提供服務支持以獲得更多信息的正確路徑

0

變化你的路徑上

$config['upload_path'] = './upload/profil/'; 

$config['upload_path'] = 'base_url("upload/profil/")';