可以't使用CI控制器将文件上载到codeigniter文件夹



我正在制作一个表单,用户可以在其中添加一张图片,它将显示在本地目录文件夹中,并显示在sql表中。但似乎我尝试了多少次,我尝试上传的图片都不会上传!请帮忙!

控制器:

<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Auth extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->database();
$this->load->library('session');
$this->load->library(array('ion_auth', 'form_validation'));
$this->load->helper(array('url', 'language', 'form'));
$this->load->model('Ion_auth_model');
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
log_message('debug', 'CI My Admin : Auth class loaded');
}
public function index() {
$data['page'] = $this->config->item('englishlivebali_template_dir_public') . "login_form";
$data['module'] = 'auth';
$this->load->view($this->_container, $data);
}
public function login_form(){
if ($this->ion_auth->logged_in()) {
redirect('student', 'refresh');
} else {
$data['page'] = $this->config->item('englishlivebali_template_dir_public') . "login_form";
$data['module'] = 'auth';
$this->load->view($this->_container, $data);
}
}
public function login_formteacher(){
if ($this->ion_auth->logged_in()) {
redirect('teacher', 'refresh');
} else {
$data['page'] = $this->config->item('englishlivebali_template_dir_public') . "login_formteacher";
$data['module'] = 'auth';
$this->load->view($this->_container, $data);
}
}
public function login() {
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == true) {
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('username'), $this->input->post('password'), $remember)) {
if ($this->input->post('username')=='teacher') {
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('/teacher/dashboard', 'refresh');
}
elseif ($this->input->post('username')!=='teacher') {
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('/student/index', 'refresh');
} 
else {
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('/student/dashboard', 'refresh');
}
} else {
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('auth/login_form', 'refresh');
}
}
}
public function logout() {
$this->ion_auth->logout();
redirect('auth', 'refresh');
}

/* End of file auth.php */
/* Location: ./modules/auth/controllers/auth.php */

public function users_save()
{
$username = $_POST["username"];
$email = $_POST["email"];
$salt       = $this->Ion_auth_model->store_salt ? $this->Ion_auth_model->salt() : FALSE;
$password = $this->Ion_auth_model->hash_password($_POST["password"], $salt);
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run()===FALSE)
{
$this->session->set_flashdata('gagal_user_save', 'Gagal Menambahkan User Baru');
redirect('auth');
}
else {
$this->db->query("INSERT INTO users (username, email, password, active) values ('$username', '$email', '$password', 1)");
$idn = $this->db->query("select id from users order by id desc limit 1")->result();
foreach ($idn as $val => $value ){
$idValue = $value->id; 
}
$this->db->query("insert into users_groups (user_id, group_id) values ('$idValue','$idgz')");
$this->session->set_flashdata('sukses_user_save', 'Sign Up Success, Now Please Log In');
redirect ('auth');
}
}
public function biodata()
{
$namad = $_POST["namad"];
$namab = $_POST["namab"];
$tempat_lahir = $_POST["tempat_lahir"];
$tgl_lahir = $_POST["tgl_lahir"];
$jk = $_POST["jk"];
$agama = $_POST["agama"];
$ayah = $_POST["ayah"];
$ibu = $_POST["ibu"];
$alamat = $_POST["alamat"];
$idz = $this->input->post('up');
$this->db->query("UPDATE users SET first_name = '$namad', last_name = '$namab', tempat_lahir = '$tempat_lahir', tgl_lahir = '$tgl_lahir', jk = '$jk', agama = '$agama', ayah = '$ayah', ibu = '$ibu', alamat = '$alamat' WHERE id = '$idz'");
redirect ('student/biodata');
}
public function do_upload()
{

$config['upload_path']          = './uploads/';
$config['allowed_types']        = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) 
{
$error = array('error' => $this->upload->display_errors());
redirect ('student/biodata');
}else{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
}
}
}

表单中的输入类型的ID为"gambar_produk",名称也为"gambar _produk"。我尝试使用的函数是do_upload函数。我试着寻找各种方法,但仍然不起作用。请帮忙!

希望这将帮助您:

注意:您的表单应该具有属性enctype="multipart/form-data",更好地使用form_open_multipart();

$this->upload->do_upload()$this->upload->do_upload('gambar_produk')关联,其中gambar_produk是文件输入的名称

你的方法do_upload应该是这样的:

public function do_upload()
{
$config['upload_path']          = './uploads/';
$config['allowed_types']        = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('gambar_produk')) 
{
$error = array('error' => $this->upload->display_errors());
redirect ('student/biodata');
}else
{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
}
}

更多信息:https://www.codeigniter.com/userguide3/libraries/file_uploading.html

默认情况下,do_upload期望文件来自名为userfile的表单字段,并且表单的类型必须为"multipart"。

要使其工作,请将输入文件的名称gambar_produk更改为userfile或使用

if ( ! $this->upload->do_upload('gambar_produk')) 
{
$error = array('error' => $this->upload->display_errors());
redirect ('student/biodata');
}else{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
}

相关内容

  • 没有找到相关文章

最新更新