我想验证上传的图片,所以当size
和type
不喜欢config
时,我想显示一些警告或可能是错误消息,我已经尝试了一些东西如下面的代码,但如果size
或type
不允许而不是显示错误消息或提醒,它只是使按钮无法点击,我做错了什么?,我已经搜索了所有通过互联网,但我仍然没有得到它因为我刚刚学习CI
和PHP
更不用说英语不是我的第一个,我相信它可以使用javascript
完成或者在controller
本身
CONTROLLER
public function Upload($id){
$upload = $this->input->post('fotoDsn');
//Foto Set
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/img/dosen/';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '1000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}else if (!$this->upload->do_upload('userfile')){
$upload = 2;
}
if($upload==1){
$data = array(
'foto_dosen'=>$photoName);
$insert = $this->MDosen->update(array('id'=>$id), $data);
if($insert){
echo 1;
}else{
echo 2;
}
}else if($upload==2){//if upload fail
alert('error');
echo "failed";
$errors = $this->upload->display_errors('<p>','failed try again','</p');
flashMsg($errors);
}
}
查看
<!-- Upload Modal Structure Start -->
<div id="modalUpload" class="modal" style="width: 40%; height: auto">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<i class="medium material-icons prefix">assignment_ind</i>
<h4 class="modal-title"> Upload Foto</h4>
</div>
<div class="modal-body">
<form action="upload" id="tambahFormUpload" method="post" enctype="multipart/form-data">
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="userfile">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" >Simpan</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- Upload Modal Structure End -->
答案 0 :(得分:0)
您的代码没有问题。它在codeigniter-3.0.6
中完美运行。查看错误日志后,我认为您使用的codeigniter版本有错误。
参考此处:https://github.com/bcit-ci/CodeIgniter/issues/4137
修复就在这里:https://github.com/bcit-ci/CodeIgniter/commit/84f24c23baf5ea45c30c4ab3cbc57cd846ea0f56
我认为您必须更改位于190
Exceptions.php
文件的行号C:\xampp\htdocs\PETTA\system\core\Exceptions.php
发件人强>
public function show_exception(Exception $exception)
要强>
public function show_exception($exception)
<强>更新强>
我再次测试了您的代码。这是您的代码的工作副本。
function upload()
{
echo'<div id="modalUpload" class="modal" style="width: 40%; height: auto">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<i class="medium material-icons prefix">assignment_ind</i>
<h4 class="modal-title"> Upload Foto</h4>
</div>
<div class="modal-body">
<form action="'.base_url('costing/upload').'" id="tambahFormUpload" method="post" enctype="multipart/form-data">
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="userfile">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" >Simpan</button>
</div>
</form>
</div>
</div>
</div>
</div>';
// $upload = $this->input->post('fotoDsn');
//Foto Set
if( $_SERVER['REQUEST_METHOD'] == 'POST' ){
$photoName = gmdate("d-m-y-H-i-s", time()+3600*7).".jpg";
$config['upload_path'] = './assets/img/dosen/';
$config['allowed_types'] = 'gif||jpg||png';
$config['max_size'] = '1000';
$config['file_name'] = $photoName;
$this->load->library('upload',$config);
if($this->upload->do_upload('userfile')){
$upload = 1;
}else if (!$this->upload->do_upload('userfile')){
$upload = 2;
}
if($upload==1){
// $data = array(
// 'foto_dosen'=>$photoName);
// $insert = $this->MDosen->update(array('id'=>$id), $data);
// if($insert){
// echo 1;
// }else{
// echo 2;
// }
}else if($upload==2){//if upload fail
// echo "failed";
$errors = $this->upload->display_errors();
echo '<script type="text/javascript">
alert("error, failed try again.'.$errors.'");
</script>';
// flashMsg($errors);
}
}
}
由于我正忙于做另一项工作,我在这里没有使用任何视图文件,只是控制器中的一个功能。您必须将form action="'.base_url('costing/upload').'"
更改为首选控制器/函数,没有名为fotoDsn
的输入,所以我已注释掉这一行:$upload = $this->input->post('fotoDsn');
,还注释掉了数据库更新部分,因为我没有拥有模型或表格,还更新了错误报告功能,因为我不知道flashMsg($errors)
功能的作用。您必须修改这些更改以适合您自己的更改。