How to clear form data after submission? - php

I have form with a file input in my CodeIgniter website. When I fill in the form and click the submit button it's working fine. However, when I refresh the browser the same form data is uploaded again. How can I prevent this ?
input form
<form name="image_upload" action="<?= base_url() ?>admin/image_move_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<ul>
<li class="ui-field"><label for="filename">File Name :</label></li>
<li class="ui-input"><input type="text" name="filename" placeholder=""></li>
<li class="ui-field"><label for="file">Image( *only .jpg) :</label></li>
<li class="ui-input"><input type="file" name="imagefile" value="" placeholder="" required=""></li>
<li><input type="submit" name="Upload" class="ui-submit"></li>
</ul>
</form>
controller section
function image_move_upload() {
$data['errors'] = NULL;
$data['success'] = NULL;
if (isset( $_POST['Upload'] )) {
$config_arr = array(
'upload_path' => './uploads/slider/',
'allowed_types' => 'jpg',
'max_size' => '2048',
'max_width' => '1024',
'max_height' => '768',
'encrypt_name' => true,
'file_name' => 'sanji.jpg',
);
$this->load->library('upload', $config_arr);
if (!$this->upload->do_upload('imagefile')) {
$data['errors'] = $this->upload->display_errors(); // this isn't working
} else {
$data['qs'] = $this->upload->data();
$data['success'] = "File Uploaded Successfully";
}
}
$data['main_content'] = 'admin/upload';
$this->load->view('admin_layout', $data);
}

Use the Post/Redirect/Get pattern. http://en.wikipedia.org/wiki/Post/Redirect/Get

redirect to same controller with refresh. Use following code.
redirect('admin/image_move_upload', 'refresh');

Use Redirect Function rather than calling a function of same class.
So, Use redirect('admin/image_move_upload', 'refresh');

Related

How to upload file with codeigniter-restserver?

I am try upload the file with Postman,but get the wrong message:You did not select a file to upload. I do not know what is the reason, but also trying to find ways.
Look at the gif of Postman:
Look at the php code:
public function upload_post()
{
$this->load->helper(array('form', 'url'));
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload',$config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->set_response($data, REST_Controller::HTTP_CREATED);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->response($error, REST_Controller::HTTP_BAD_REQUEST);
}
}
Look at the html:
<?php echo $error;?>
<?php echo form_open_multipart('api/example/upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
Upload file with the browser is OK!
Two code is OK!Browser upload no problem!But can not upload with Postman!
Has been solved, the original is not added $this->upload->do_upload('file') and key file postman.
could you put your form code ? i think you forgot to add enctype='multipart/form-data'

How to upload an image and save it to database in Codeigniter?

I know this question has been asked millions of times, but all of the questions are a little bit too difficult for beginners.
So what I'm looking for is how to create a simple button that uploads an image and get its full_path and save it in a hidden input of the form, so when i click submit, the form will insert the path with the other inputs, and so the image would have been uploaded to a directory.
It's a simple project, a basic blog page, where I need the following the inputs to be saved:
1. Image
2. Title
3. Body
The only thing i couldn't accomplish is the image part.
So here's the view code:
<h1>Add New Post</h1>
<br />
<?=form_open('admin/add_post');?>
<p>
<input type="text" name="title" />
</p>
<input hidden="hidden" name="date" value="<?php echo date('d-m-y');?>"/>
<p>
<textarea id="textarea" name="body" rows="10"></textarea>
</p>
<p>
<input type="submit" value="Submit Post" />
</p>
</form>
and here's the controller when you click Submit:
// Adds new posts to the database table
function add_post()
{
$this->form_validation->set_rules('title', 'Title', 'required|min_length[2]');
$this->form_validation->set_rules('body', 'Body', 'required|min_length[2]');
if ($this->form_validation->run() == TRUE) {
// INSERTING GOES HERE..
$this->db->insert('entries', $_POST);
redirect('admin/posts');
}
else {
redirect('admin/add');
}
}
So please I need someone to tell me how to write the upload function to upload the image on the same page and on the same form, and save its path to the database.
I can make it save the path, but i wanna get the path first and add it to the same form in a hidden input, EX:
Please I really need your help, and if you have any idea on how to help me, please post your answer.
Thanks in advance.
It's very simple to achieve this with CodeIgniter.
I'll use the Version 3.1.0 reference:
View:
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
Controller:
public function do_upload() {
$fileData = array();
// File upload script
$this->upload->initialize(array(
'upload_path' => './uploads/',
'overwrite' => false,
'max_filename' => 300,
'encrypt_name' => true,
'remove_spaces' => true,
'allowed_types' => 'gif|jpg|png',
'max_size' => 100,
'xss_clean' => true,
));
$this->form_validation->set_rules('title', 'Title', 'required|min_length[2]');
$this->form_validation->set_rules('body', 'Body', 'required|min_length[2]');
if ($this->form_validation->run() == TRUE) {
if ($this->upload->do_upload('userfile')) {
$data = $this->upload->data(); // Get the file data
$fileData[] = $data; // It's an array with many data
// Interate throught the data to work with them
foreach ($fileData as $file) {
$file_data = $file;
}
var_dump($file_data);
$this->db->insert('entries', array(
// So you can work with the values, like:
'title' => $this->input->post('title', true), // TRUE is XSS protection
'body' => $this->input->post('body', true),
'file_name' => $file_data['file_name'],
'file_ext' => $file_data['file_ext'],
));
$this->session->set_flashdata('success', 'Form submitted successfully');
redirect('admin/add');
} else {
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect('admin/add');
}
} else {
$this->session->set_flashdata('error', validation_errors());
redirect('admin/add');
}
}
You can use var_dump to see available data to work, or see the documentation (on the bottom)
https://www.codeigniter.com/userguide3/libraries/file_uploading.html

issues in image uploading with php codeigniter

I have some problem in image uploading with php codeigniter.
this is my controller
public function FunctionName()
{
$profile_pic = $this->input->post('profile_pic');
echo $profile_pic;
}
It is a simple function and I just want to see the name of the file (image) which I select.
this is my view
<form class="" method="post" action="<?=base_url();?>Controller/FunctionName" enctype="multipart/form-data">
<input type="file" name="profile_pic">
<input type="submit" name="submit">
</form>
When I run this script. It shows me a blank page. I cannot get/display the name of the file which I selected. Note that I have auto-loaded the helper. E.g.
$autoload['helper'] = array('url', 'file', 'date', 'form');
$file=$_FILES['file_name']['name'];
$config = array('file_name'=>$file,
'upload_path' => "./folder/",
'allowed_types' => "jpg|jpeg|png",
'remove_spaces' => FALSE,
'overwrite' => TRUE
);
$this->load->library('upload', $config);
$this->upload->do_upload('file_name');

Uploading file with php

I have the following set up in a Code Igniter application, however the file is not being passed through to the file uploader class. This is the second form on the page, just for reference and I have checked that they are both closed correctly.
The output I am getting is You did not select a file to upload. when I have been selecting an valid image.
View
<form action="../albums/upload" class="form-horizontal" method="post">
<div class="form-group">
<input type="file" name="userfile" required="" class="form-control">
</div>
<div class="form-group">
<div class="col-md-4 col-md-offset-4">
<input type="submit" id="upload" name="upload" class="btn btn-success" value="Upload">
</div>
</div>
</form>
Controller
public function upload() {
if($this->input->post('userfile')){
$this->model_photo->do_upload();
}
}
Model
function do_upload() {
$path = base_url().'res/image/';
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png|tif|tiff',
'upload_path' => $path
);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
echo $error['error']; //echo debugging purposes
} else {
$data = array('upload_data' => $this->upload->data());
echo $data['upload_data']; //echo debugging purposes
}
}
in your <form> add enctype="multipart/form-data"
<form action="../albums/upload" class="form-horizontal" method="post" enctype="multipart/form-data">

image uploading is not working in CodeIgniter

when I click button then it will return error "You did not select a file to upload." and My image is not uploaded!
now please help me what can I do?
my view is
<form action="insertform" enctype="multipart" name="form" method="post" id="contact-form" class="form-horizontal">
<div class="control-group">
<label class="control-label" for="name">Image Upload:</label>
<div class="controls">
<input type="file" class="input-large" name="image" id="image">
</div>
<form>
my controller is:
public function upload()
{
$this->load->helper(array('form', 'url'));
$config['upload_path'] = FCPATH."upload";
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('xyz', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('xyz', $data);
}
}
I think the file input name should be "userfile", not "image".
CI file upload file control default name is userfile.
If you want to change the it's name to customized one, then you have to pass the new name to upload function like :
if ( ! $this->upload->do_upload("image")) // image is your new name for file control
Firstly,
either change the do_upload function call to:
if ( ! $this->upload->do_upload('image'))
OR
change the input name to this:
<input type="file" class="input-large" name="userfile" id="image">
Secondly,
change your form enctype to:
enctype="multipart/form-data"

Categories