I have the following code witch I have just found out that I can upload any kind of file (.png.jpg .txt etc) but I am also unsure were to place $data[success] = TRUE
so that it shows after the page has been reloaded and the functions have been run. How could I also do this with the error messages if there was an error?
Controller:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Addimage extends CI_Controller {
function __construct(){
parent::__construct();
}
function index() {
if(!$this->session->userdata('logged_in')) {
redirect('admin/home');
}
// Main Page Data
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Add Gallery Image';
$data['content'] = $this->load->view('admin/addimage',NULL,TRUE);
$this->load->view('admintemplate', $data);
//Set Validation
//$this->form_validation->set_rules('userfile', 'userfile', 'trim|required');
$this->form_validation->set_rules('description', 'Description', 'trim|required');
if($this->form_validation->run() === TRUE) {
//Set File Settings
$config['upload_path'] = 'includes/uploads/gallery/';
$config['allowed_types'] = 'jpg|png';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()) {
$error = array('imageError' => $this->upload->display_errors());
}
else{
$data = array('upload_data' => $this->upload->data());
$data['success'] = TRUE;
$config['image_library'] = 'GD2';
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['new_image'] = 'includes/uploads/gallery/thumbs/';
$config['create_thumb'] = 'TRUE';
$config['thumb_marker'] ='_thumb';
$config['maintain_ratio'] = 'FALSE';
$config['width'] = '200';
$config['height'] = '150';
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
$file_info = $this->upload->data();
$data = array(
'description' => $this->input->post('description', TRUE),
'imagename' => $file_info['file_name'],
'thumbname' =>$file_info['raw_name'].'_thumb'.$file_info['file_e开发者_如何学Goxt']
);
$this->image_model->addImage($data);
}
}
}
View:
<?php
//Setting form attributes
$formAddImage = array('id' => 'addImage', 'name' => 'addImage');
$imageImage = array('id' => 'userfile', 'name' => 'userfile', 'placeholder' => 'File Location*');
$imageDescription = array('id' => 'description','name' => 'description','placeholder' => 'Image Description*');
if($success == TRUE) {
echo '<section id = "validation">Image Uploaded</section>';
}
?>
<section id = "validation"><?php echo validation_errors();?></section>
<?php
echo form_open_multipart('admin/addimage', $formAddImage);
echo form_fieldset();
echo form_upload($imageImage);
echo form_textarea($imageDescription);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
?>
if(!$this->upload->do_upload()) {
$status = array('imageError' => $this->upload->display_errors());
}
else
{
$status = 'Success message'; // or just true
// etc
}
then in your view:
if($status==TRUE)
{
// your success
}
else
{
// aw..somethings's gone wrong, print_r($status);
{
So essentially use one variable.
This is also a good example of when to use flashdata, which is exactly for these purposes.
if(!$this->upload->do_upload())
{
$this->session->set_flashdata('message', 'Sorry, error...');
}
else
{
$this->session->set_flashdata('message', 'Success!');
}
You can of course pass your array of data to set_flashdata
if needed.
精彩评论