我尝试使用Codeigniter和AJAX上传文件,但我的表单始终显示错误:
上传路径不正确。
模型
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>
的的Ajax
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
}
});
}
当我单击表单中的“保存”按钮时,表单始终显示以下错误:
上传错误:上传路径似乎无效。
答案 0 :(得分:1)
在公共服务器托管(例如共享托管服务提供商)上,您需要提供最有可能提供上传文件夹的完整相对路径,这与localhost环境不同。还要确保您具有将文件写入该目录的所有权限
您可以在localhost环境中使用
$config['upload_path'] = './upload/profil';
但是在共享主机上,您需要更具体,通常是
$config['upload_path'] = '/home/yourserver/public_html/upload/profil';
您可以找到此upload_path,例如在您的帐户左栏中的cPanel主页上,或者可能想要调用您的提供商帮助台以获取有关正确路径的更多信息
答案 1 :(得分:0)
改变你的路径
$config['upload_path'] = './upload/profil/';
到
$config['upload_path'] = 'base_url("upload/profil/")';