Javascript 输入类型文件在CodeIgniter中不工作
这是控制器。我更新了图像,但我认为输入类型文件不接受文件或不将文件发送到控制器,因此我的代码无法正常工作。我认为控制器代码是正确的Javascript 输入类型文件在CodeIgniter中不工作,javascript,codeigniter,Javascript,Codeigniter,这是控制器。我更新了图像,但我认为输入类型文件不接受文件或不将文件发送到控制器,因此我的代码无法正常工作。我认为控制器代码是正确的 class OurTeam extends CI_Controller { public function UpdateTeam() { $config['image_library'] = 'gd2'; $config['upload_path'] = '../employeephoto/';
class OurTeam extends CI_Controller {
public function UpdateTeam()
{
$config['image_library'] = 'gd2';
$config['upload_path'] = '../employeephoto/';
$config['source_image'] ='employeephoto/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 12024;
$config['width'] = 300;
$config['height'] = 150;
$this->load->library('upload', $config);
$img = $_FILES['fileupdate']['name'];
if($img)
{
if (!$this->upload->do_upload('fileupdate'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
$file_name=($this->upload->data('file_name'));
$this->load->model('OurTeamModel');
$id= $_POST['id'];
$removeimg = $this->OurTeamModel->SelectById($id);
echo $imgpath = 'employeephoto/'.$removeimg[0]->img;
if(file_exists($imgpath))
{
unlink($imgpath);
}
else
{
echo "no";
}
$this->OurTeamModel->UpdateOurTeam($file_name);
$config['image_library'] = 'gd2';
$config['upload_path'] = '../employeephoto/'.$file_name;
$config['source_image'] = '../employeephoto/'.$file_name;
$config['maintain_ratio'] = false;
$config['allowed_types'] = 'gif|jpg|png';
$config['width'] = 660;
$config['height'] = 300;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$ok = $this->image_lib->resize();
}
}
else
{
$this->load->model('OurTeamModel');
$this->OurTeamModel->UpdateOurTeamRecord();
}
redirect('admin/OurTeam');
}
}
这是查看页面
<?php echo form_open_multipart('admin/OurTeam/UpdateTeam');?>
<input type="file" name="fileupdate" size="20" id="fileupdate">
<input type="submit" value="Save" >
</form>
请更改为
发件人:
$config['image_library'] = 'gd2';
$config['upload_path'] = '../employeephoto/';
$config['source_image'] ='employeephoto/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 12024;
$config['width'] = 300;
$config['height'] = 150;
致:
我已经查过你的密码了。请做一件事,这篇文章,并遵循一步一步。希望这会有意义
以下是如何在CodeIgniter中上传文件的分步说明。试试这个,让我知道输出的内容(可能需要打印SRC并上传到imgur):
公共函数updatetam(){
$config['image_library']='gd2';
$config['upload_path']='../employeephoto/';
//$config['source_image']='employeephoto/';
$config['allowed_types']='gif | jpg | png';
$config['max_size']=12024;
$config['width']=300;
$config['height']=150;
$this->load->library('upload',$config);
如果(!空($_文件['fileupdate']['name'])){
如果(!$this->upload->do_upload('fileupdate')){
echo“上载失败,错误:
”;
退出($this->upload->display_errors());
}否则{
echo“上传已通过
”;
$file_name=($this->upload->data('file_name');
$this->load->model('OurTeamModel');
$id=$_POST['id'];
$removeimg=$this->OurTeamModel->SelectById($id);
echo$imgpath='employeephoto/'。$removeimg[0]->img;
如果(文件_存在($imgpath)){
取消链接($imgpath);
}否则{
回应“否”;
}
$this->OurTeamModel->updateOuteam($file\u name);
回显“调整图像大小
”;
$config['image_library']='gd2';
$config['upload\u path']='../employeephoto/'.$file\u name;
$config['source\u image']='../employeephoto/'.$file\u name;
$config['mainter_ratio']=false;
$config['allowed_types']='gif | jpg | png';
$config['width']=660;
$config['height']=300;
$this->load->library('image_lib',$config);
$this->image_lib->clear();
$this->image\u lib->initialize($config);
如果(!$this->image\u lib->resize()){
回显“图像大小调整失败
”;
退出($this->image\u lib->display\u errors());
}
呼应“一切都好
”;
}
}否则{
回显“无文件
”;
$this->load->model('OurTeamModel');
$this->OurTeamModel->updateOutertamRecord();
}
//重定向(“管理员/我们的团队”);
}
您有任何错误吗?您是否在提交后在视图上打印$this->upload->display_errors()
?视图页面@GufranHasanI中没有显示错误?在视图页面中写入此代码$this->upload->display_errors(),但未显示错误@BadiParmagi您不能像那样在视图中显示该错误。好吧,我们很快就能解决这个问题了@ChandraPrakashPanwar,如果条件允许打印($error)就可以进入;出口在其他打印中($data);exitshow此错误bro遇到PHP错误严重性:注意消息:未定义变量:错误文件名:controllers/OurTeam.PHP行号:214回溯:文件:D:\xampp\htdocs\techtoconnect\admin\application\controllers\OurTeam.PHP行:214函数:\错误\处理程序文件:D:\xampp\htdocs\techtoconnect\admin\index.PHP行:315功能:仅此解决方案不起作用。显示此错误>上载失败,错误:上载路径似乎无效。那么,您的错误就在于此。我没有对你的代码做任何修改,只是添加了一些回音。但是当我早些时候问你显示错误函数打印的是什么时,你什么也没说,但这显然是很重要的。您需要在指定路径的任何地方使用类似于/employeephoto/
的路径,并在index.php所在的目录中创建该名称的文件夹。那么一切都会好起来的。
$config['upload_path'] = './employeephoto/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
public function UpdateTeam() {
$config['image_library'] = 'gd2';
$config['upload_path'] = '../employeephoto/';
//$config['source_image'] = 'employeephoto/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 12024;
$config['width'] = 300;
$config['height'] = 150;
$this->load->library('upload', $config);
if (!empty($_FILES['fileupdate']['name'])) {
if (!$this->upload->do_upload('fileupdate')) {
echo 'upload failed with error: <br>';
exit($this->upload->display_errors());
} else {
echo 'upload passed <br>';
$file_name = ($this->upload->data('file_name'));
$this->load->model('OurTeamModel');
$id = $_POST['id'];
$removeimg = $this->OurTeamModel->SelectById($id);
echo $imgpath = 'employeephoto/' . $removeimg[0]->img;
if (file_exists($imgpath)) {
unlink($imgpath);
} else {
echo "no";
}
$this->OurTeamModel->UpdateOurTeam($file_name);
echo 'resizing image <br>';
$config['image_library'] = 'gd2';
$config['upload_path'] = '../employeephoto/' . $file_name;
$config['source_image'] = '../employeephoto/' . $file_name;
$config['maintain_ratio'] = false;
$config['allowed_types'] = 'gif|jpg|png';
$config['width'] = 660;
$config['height'] = 300;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
if (!$this->image_lib->resize()) {
echo 'image resize failed <br>';
exit($this->image_lib->display_errors());
}
echo 'all good <br>';
}
} else {
echo 'no file <br>';
$this->load->model('OurTeamModel');
$this->OurTeamModel->UpdateOurTeamRecord();
}
//redirect('admin/OurTeam');
}