我尝试使用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>
阿贾克斯
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
}
});
}
当我单击表单中的保存按钮时,表单始终显示此错误:
上载错误:上载路径似乎无效。
参考方案
在公共服务器托管(例如共享托管提供商)上,您很有可能需要提供上载文件夹的完整相对路径,这与本地主机环境不同。还要确保您拥有将文件写入该目录的所有权限
您可以在本地主机环境上使用
$config['upload_path'] = './upload/profil';
但是在共享主机上,您需要获取更具体的信息,通常是这样的
$config['upload_path'] = '/home/yourserver/public_html/upload/profil';
您可以找到此upload_path,例如在您帐户的cPanel主页上的左列,或者可能要致电您的提供商帮助台以获取有关正确路径的更多信息
打印二维阵列 - javascript我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…
保留文本区域的数据或值,然后选择输入 - javascript通过$ _POST提交表单时,输入文件值仍然保留。像这样: if($_POST){ $error = false; if(!$post['price']){ $error = true; $error_note['price'] = "*Should not be empty"; } if($err…
变更事件时道具无法正常工作 - javascript我有一些代码,当用户从下拉列表中选择一个项目时,这会触发一个change事件,然后调用php页面进行处理。首次加载页面时,我选择一个项目,并在触发change事件时,这应更改所选select输入的占位符并禁用相同的输入:“#box_ffrtv”。但是,发生的事情是更改仅在我在下拉菜单中进行了第二选择之后才发生,然后更改了占位符并禁用了输入。我对jquery还…
将简单的javascript代码转换为c# - javascript昨天我在这里问了一个问题。使用javascript和html解决方案很简单前一阵子我打算什么是操纵html来执行javascript中的任务但是我改变了主意,将javascript代码重写为c#这是输入<Abstract> <Heading>Abstract</Heading> <Para TextBreak=…
包含“。”的onClick函数参数 - javascript我正在尝试创建一个表,该表包含该表的“更改密码”列中各项的onClick函数,以便我的系统管理员可以更改每个人的密码。每个onClick都会调用函数“ ChangePassOpen”,该函数将打开一个带有新密码输入框和另一个按钮的模式,以实际调用该函数来更改密码。为了使我的程序能够识别管理员正在更改的帐户,我需要将用户名作为参数传递,但是我的用户名包含“。”…