上传照片失败 Codeigniter [英] Upload Photo Failed Codeigniter

查看:22
本文介绍了上传照片失败 Codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我的照片无法插入数据库.

实际上我想使用 codeigniter 进行在线考试.我想用图片上传问题.但是当我尝试上传图片时,代码不起作用.

但问题成功插入到数据库中.只有图片上传失败

控制器:

function insert(){$nama_asli = $_FILES['userfile']['name'];$config ['file_name'] = $nama_asli;$config ['upload_path'] = './images';$config ['allowed_types'] = 'gif|jpg|png|jpeg';$config ['max_size'] = '2500';$this->load->library('upload', $config);$upload_data = $this->upload->data();$file_name = $upload_data['file_name'];$id_soal = '';$soal = $_POST['soal'];$a = $_POST['a'];$b = $_POST['b'];$c = $_POST['c'];$d = $_POST['d'];$kunci = $_POST['kunci'];$status = $_POST['status'];$数据=数组('id_soal' =>$id_soal,'soal' =>$soal,'a' =>$a,'b' =>$b,'c' =>$c,'d' =>$d,'kunci' =>$kunci,'状态' =>$状态,'照片' =>$文件名,);$hasil = $this->soal_model->Simpan('soal', $data);如果($hasil>=1){重定向('仪表板/索引',$数据);}}

型号:

class Soal_model extends Ci_Model {公共函数 Ambil($where="") {$data = $this->db->query('select * from soal '.$where);返回 $data;}公共函数 Simpan($table, $data){$res = $this->db->insert($table, $data);返回 $res;}

查看:

<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data"><form class="form-horizo​​ntal" method="post" style="margin : 10px;"><div类=行"><div class = "col-sm-offset-3 col-sm-6"><div class="form-group"><label>Soal :</label><textarea type="text" class="form-control" name="soal" id="soal" required></textarea>

<div class="form-group"><label>Jawaban A :</label><input type="text" class="form-control"placeholder="Type Here" name="a" id="a" required/>

<div class="form-group"><label>Jawaban B :</label><input type="text" class="form-control"placeholder="Type Here" name="b" id="b" required/>

<div class="form-group"><label>Jawaban C :</label><input type="text" class="form-control"placeholder="Type Here" name="c" id="c" required/>

<div class="form-group"><label>Jawaban D :</label><input type="text" class="form-control"placeholder="Type Here" name="d" id="d" required/>

<div class="form-group"><label>Kunci :</label><select name="kunci" id="kunci" class="form-control"><option>选择</option><option>A</option><option>B</option><option>C</option><option>D</option></选择>

<div class="form-group"><label>状态:</label><select name="status" id="status" class="form-control"><option value="">选择</option><option value="tampil">Tampil</option><option value="tidak">Tidak</option></选择><div class="form-group"><标签>照片:</标签><input type="file" name="foto" id="foto" size="20"/>

<br><div class="form-group"><button type="submit" class="btn btn-default">Simpan</button><button type="reset" class="btn btn-default">Hapus</button>

</表单>

数据库:

解决方案

请使用此代码

<?php if (!defined('BASEPATH')) exit('不允许直接脚本访问');类仪表板扩展 CI_Controller{函数__构造(){parent::__construct();$this->load->model('soal_model');}函数插入(){$config =数组('上传路径' =>'./图片','allowed_types' =>'gif|jpg|png|jpeg','max_size' =>'2500',);$this->load->library('upload', $config);$this->upload->do_upload('file_upload');$upload_data = $this->upload->data();$file_name = $upload_data['file_name'];$数据=数组('照片' =>$文件名,);$hasil = $this->soal_model->Simpan('soal', $data);如果($hasil>=1){重定向('仪表板/索引',$数据);}}?>

This is my code, my photo can't be inserted into the database.

Actually I want to make an online exam with the codeigniter. I want to upload the question with the pic. but when I tried to upload the pict, the code is not working.

but the question success inserted into the database. only the pict failed to upload

Controller:

function insert(){
		$nama_asli = $_FILES['userfile']['name'];
		$config ['file_name'] = $nama_asli;
		$config ['upload_path'] = './images';
		$config ['allowed_types'] = 'gif|jpg|png|jpeg';
		$config ['max_size'] = '2500';
			
		$this->load->library('upload', $config);
			
		$upload_data = $this->upload->data();
		$file_name = $upload_data['file_name'];	
		
		$id_soal = '';
		$soal = $_POST['soal'];
		$a = $_POST['a'];
		$b = $_POST['b'];
		$c = $_POST['c'];
		$d = $_POST['d'];
		$kunci = $_POST['kunci'];
		$status = $_POST['status'];

		$data = array(
			'id_soal' => $id_soal,
			'soal' => $soal,
			'a' => $a,
			'b' => $b,
			'c' => $c,
			'd' => $d,
			'kunci' => $kunci,
			'status' => $status,
			'foto' => $file_name,
			);

		$hasil = $this->soal_model->Simpan('soal', $data);

		if($hasil>=1){
			redirect('dashboard/index', $data);
		}
	}

Model:

class Soal_model extends Ci_Model {

  public function Ambil($where= "") {
    $data = $this->db->query('select * from soal '.$where);
    return $data;
  }
  
  public function Simpan($tabel, $data){
    $res = $this->db->insert($tabel, $data);
    return $res;
  }

View:

<form role="form" action="<?php echo base_url(); ?>dashboard/insert" method="POST" enctype="multipart/form-data">
<form class="form-horizontal" method="post" style = "margin : 10px;">
		<div class = "row">
		<div class = "col-sm-offset-3 col-sm-6">
		<div class="form-group">
		<label>Soal :</label>
		<textarea type="text" class="form-control" name="soal" id="soal" required></textarea>
		</div>
		
		<div class="form-group">
		<label>Jawaban A :</label>
		<input type="text" class="form-control" 
		placeholder="Type Here" name="a" id="a" required/>
		</div>
		
		<div class="form-group">
		<label>Jawaban B :</label>
		<input type="text" class="form-control" 
		placeholder="Type Here" name="b" id="b" required/>
		</div>
		
		<div class="form-group">
		<label>Jawaban C :</label>
		<input type="text" class="form-control" 
		placeholder="Type Here" name="c" id="c" required/>
		</div>
		
		<div class="form-group">
		<label>Jawaban D :</label>
		<input type="text" class="form-control" 
		placeholder="Type Here" name="d" id="d" required/>
		</div>
		
		<div class="form-group">	
		<label>Kunci :</label>
		<select name="kunci" id="kunci" class="form-control">
		<option>Select</option> 
		<option>A</option>  
		<option>B</option> 
		<option>C</option>
		<option>D</option>
		</select> 
		</div>
		
		<div class="form-group">	
		<label>Status :</label>
		<select name="status" id="status" class="form-control">
		<option value="">Select</option> 
		<option value="tampil">Tampil</option>  
		<option value="tidak">Tidak</option> 
		</select> 
		
		<div class="form-group">	
		<label>Photo :</label>
		<input type="file" name="foto" id="foto" size="20"/>
		</div>
		<br>
		
		<div class="form-group">	
        <button type="submit" class="btn btn-default">Simpan</button>
		<button type="reset" class="btn btn-default">Hapus</button>
		</div>
		</div>
		</div>
	</form>
	

Database:

解决方案

Kindly use this code

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller{
    function __construct(){
        parent::__construct();
        $this->load->model('soal_model');
}

function insert()
{
    $config =array(
        'upload_path' => './images',
        'allowed_types' => 'gif|jpg|png|jpeg',
        'max_size' => '2500',
    );
    $this->load->library('upload', $config);

    $this->upload->do_upload('file_upload');

    $upload_data = $this->upload->data();   

    $file_name = $upload_data['file_name'];
    $data = array(
        'foto' => $file_name,
    );

    $hasil = $this->soal_model->Simpan('soal', $data);

    if($hasil>=1){
        redirect('dashboard/index', $data);
    }
}   
?>

这篇关于上传照片失败 Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆