Php 在codeigniter中上载多个图像的回调表单验证问题
我正在尝试方法add for,我需要上传图像,所以我进行了4次回调来上传图像,但这似乎不像我预期的那样,我对form_验证的回调无法正常运行,它无法上传4个图像,只能上传其中两个图像。我的回电有什么问题吗?我还能用callback上传四张图片吗 控制器:Php 在codeigniter中上载多个图像的回调表单验证问题,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我正在尝试方法add for,我需要上传图像,所以我进行了4次回调来上传图像,但这似乎不像我预期的那样,我对form_验证的回调无法正常运行,它无法上传4个图像,只能上传其中两个图像。我的回电有什么问题吗?我还能用callback上传四张图片吗 控制器: <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Produk extends CI_Controller { private
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Produk extends CI_Controller {
private $_uploadMethod = NULL;
public function __construct()
{
parent::__construct();
if (!$this->session->userdata('status') == 'logged_in') {
redirect('auth');
}
$this->load->model('admin/Produk_model');
}
public function index()
{
$data['title'] = 'Produk';
$data['produk'] = $this->Produk_model->getAll();
$this->template->load('back/template/template', 'back/admin/produk/produk', $data);
}
public function add()
{
$data['title'] = 'Tambah Produk';
$data['kategori'] = $this->Produk_model->getAllKategori();
$data['ikm'] = $this->Produk_model->getAllIkm();
$this->_uploadMethod = 'add';
$this->form_validation->set_rules('nama_produk', 'Nama Produk', 'trim|required|max_length[100]');
$this->form_validation->set_rules('berat', 'Berat', 'trim|required|max_length[12]');
$this->form_validation->set_rules('deskripsi', 'Deskripsi Produk', 'trim|required');
$this->form_validation->set_rules('kategori_produk', 'Kategori Produk', 'trim|required');
$this->form_validation->set_rules('ikm_produk', 'IKM Produk', 'trim|required');
$this->form_validation->set_rules('foto_1', 'Gambar Produk 1', 'callback_imageValidation1');
$this->form_validation->set_rules('foto_2', 'Gambar Produk 2', 'callback_imageValidation2');
$this->form_validation->set_rules('foto_3', 'Gambar Produk 3', 'callback_imageValidation3');
$this->form_validation->set_rules('foto_4', 'Gambar Produk 4', 'callback_imageValidation4');
if ($this->form_validation->run() == FALSE) {
$this->template->load('back/template/template', 'back/admin/produk/add', $data);
} else {
$post = $this->input->post(NULL, TRUE);
var_dump($post);
}
}
public function delete($id_produk = NULL)
{
if ($id_produk == NULL) redirect('admin/produk');
}
public function imageValidation1()
{
if ($this->_uploadMethod == NULL) {
redirect('admin/produk');
}
$config['upload_path'] = './data/produk/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 1024*2;
$config['overwrite'] = TRUE;
$config['max_filename'] = 240;
$config['file_name'] = $this->input->post('nama_produk').'_g1'.'_'.time();
$this->load->library('upload', $config);
if (!$this->upload->do_upload('foto_1')) {
$this->form_validation->set_message('imageValidation1', $this->upload->display_errors());
if ($this->_uploadMethod == 'edit') {
if ($_FILES['foto_1']['error'] != 4) {
return FALSE;
}
} else {
return FALSE;
}
} else {
return TRUE;
}
}
public function imageValidation2()
{
if ($this->_uploadMethod == NULL) {
redirect('admin/produk');
}
$config['upload_path'] = './data/produk/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 1024*2;
$config['overwrite'] = TRUE;
$config['max_filename'] = 240;
$config['file_name'] = $this->input->post('nama_produk').'_g2'.'_'.time();
$this->load->library('upload', $config);
if (!$this->upload->do_upload('foto_2')) {
$this->form_validation->set_message('imageValidation2', $this->upload->display_errors());
if ($this->_uploadMethod == 'edit') {
if ($_FILES['foto_2']['error'] != 4) {
return FALSE;
}
} else {
return FALSE;
}
} else {
return TRUE;
}
}
public function imageValidation3()
{
if ($this->_uploadMethod == NULL) {
redirect('admin/produk');
}
$config['upload_path'] = './data/produk/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 1024*2;
$config['overwrite'] = TRUE;
$config['max_filename'] = 240;
$config['file_name'] = $this->input->post('nama_produk').'_g3'.'_'.time();
$this->load->library('upload', $config);
if (!$this->upload->do_upload('foto_3')) {
$this->form_validation->set_message('imageValidation3', $this->upload->display_errors());
if ($this->_uploadMethod == 'edit') {
if ($_FILES['foto_3']['error'] != 4) {
return FALSE;
}
} else {
return FALSE;
}
} else {
return TRUE;
}
}
public function imageValidation4()
{
if ($this->_uploadMethod == NULL) {
redirect('admin/produk');
}
$config['upload_path'] = './data/produk/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 1024*2;
$config['overwrite'] = TRUE;
$config['max_filename'] = 240;
$config['file_name'] = $this->input->post('nama_produk').'_g4'.'_'.time();
$this->load->library('upload', $config);
if (!$this->upload->do_upload('foto_4')) {
$this->form_validation->set_message('imageValidation4', $this->upload->display_errors());
if ($this->_uploadMethod == 'edit') {
if ($_FILES['foto_4']['error'] != 4) {
return FALSE;
}
} else {
return FALSE;
}
} else {
return TRUE;
}
}
}
您不需要为图像验证创建多个回调。你只需要调用它一次。有没有办法我仍然可以使用多重回调?我需要在viewyes中显示输入type=“file”下面的form_error,只需调用它repeatedlyso为什么我的代码可以正常运行?请查看您的apache配置。