I had looked for many examples about it and I did something as I can do.
I will build a registration form. There will be five text fields and also a photo upload section.
what I did
The text fields are stored in the database. In here only one field is shown. (successfully)
The photo is stored in the folder after the form submitted (successfuly)
I need you to help me
The name of the photo is not stored in the database. it is stored on the folder.
The codes below.
Model
public function registration($post)
{
$this->db->insert('registration', $post);
}
Controller
function do_upload()
{
$post = $this->input->post();
$this->form_validation->set_rules('Name','Name','trim|required|xss_clean');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$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('example/registration_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$result = $this->register_model->registration($post);
$this->load->view('upload_success', $data, $result);
}
}
View
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br />
<input type="text" name="name" class="form-control" placeholder="Your name">
<input type="submit" value="upload" />
</form>
It is photo_name on the database. It will be written on controller or model? How do I do in basically?
You need to use a variable for $this->do_upload->data() if you would like to insert content into database.
Example: $data_file = $this->do_upload->data();
Example: $data_file['file_name']
$data = array(
'file_name' => $data_file['file_name'],
'file_type' => $data_file['file_type'],
'full_path' => $data_file['full_path'],
'raw_name' => $data_file['raw_name'],
'orig_name' => $data_file['orig_name'],
'client_name' => $data_file['client_name'],
'file_ext' => $data_file['file_ext'],
'file_size' => $data_file['file_size'],
'is_image' => $data_file['is_image'],
'image_width' => $data_file['image_width'],
'image_height' => $data_file['image_height'],
'image_type' => $data_file['image_type'],
'image_size_str' => $data_file['image_size_str']
);
$this->db->where('whatever', $whatever);
$this->db->update('tablename', $data);
Or
$data = array(
'file_name' => $data_file['file_name'],
'file_type' => $data_file['file_type'],
'full_path' => $data_file['full_path'],
'raw_name' => $data_file['raw_name'],
'orig_name' => $data_file['orig_name'],
'client_name' => $data_file['client_name'],
'file_ext' => $data_file['file_ext'],
'file_size' => $data_file['file_size'],
'is_image' => $data_file['is_image'],
'image_width' => $data_file['image_width'],
'image_height' => $data_file['image_height'],
'image_type' => $data_file['image_type'],
'image_size_str' => $data_file['image_size_str']
);
$this->db->insert('tablename', $data);
Related
I have a problem while uploading image files.
This my view file contents
<form action="<?php base_url()."profile/upload"?>" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="id_akun" value="<?php echo $query->id_akun?>"size="20" />
<input type="file" name="userfile" required>
<input type="submit" name="submit" value="Upload" class="btn btn-success" />
</form>
Here is my controller (profile)
public function upload() {
$config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/upload/",
'upload_url' => base_url()."upload/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => false,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $this->config);
if($this->upload->do_upload()) {
echo "file upload success";
} else {
echo "file upload failed";
}
}
When I try an upload, the image did not get upload, and there is no error shown. URL in my address bar looks like this
http://localhost/mataramtest/profile?id_akun=6&userfile=1.PNG&submit=Upload
There are several issues on your code. Revised and given below
public function upload()
{
$config = array(
'upload_path' => "./upload/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => false,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
echo "file upload success";
}
else
{
echo "file upload failed";
}
}
In view file
<form action="<?php echo site_url("profile/upload");?>" method="POST" enctype="multipart/form-data" >
<!--rest of the code is ok-->
public function img_upload()
{
$config = array(
'upload_path' => "uploads",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "3000",
'max_width' => "3000"
);
$this->upload->initialize($config);
$this->load->library('upload', $config);
if($this->upload->do_upload()) {
$response = array('upload_data' => $this->upload->data());
}
else{
$error = array('error'=>$this->upload->display_errors());
print_r($error);
}
}
i create image module and i edit image more then 1mb then can not show errormsg.
i used codigniter fremwork.
controller:
public function edit($id) {
$this->edit_status_check($id);
$this->form_validation->set_rules('agent_name', 'Agent Name', 'required');
$this->form_validation->set_rules('mobile', 'Mobile No.', 'required');
$this->form_validation->set_rules('agent_vehicle', 'Agent Vehicle', 'required');
if ($this->form_validation->run() == FALSE) {
$data = array(
'page_title' => 'Edit Agent',
'page_name' => 'agent/edit',
'result' => $this->agent_model->select_id($id),
'result_vehicle' => $this->vehicle_model->list_all(),
'error' => validation_errors(),
'id' => $id
);
$this->load->view('template', $data);
} else {
$config['upload_path'] = '../uploads/agent/';
$config['allowed_types'] = 'jpg|jpeg';
$config['encrypt_name'] = TRUE;
$config['max_size'] = 1000; // 1 mb
$this->load->library('upload', $config);
if (!empty($_FILES['agent_image']['name'])) {
if ($this->upload->do_upload('agent_image')) {
$_POST['agent_img_url'] = 'uploads/agent/' . $this->upload->data('file_name');
} else {
$data = array(
'page_title' => 'Edit Agent',
'page_name' => 'agent/edit',
'result' => $this->agent_model->select_id($id),
'result_vehicle' => $this->vehicle_model->list_all(),
'error' => $this->upload->display_errors(),
'id' => $id
);
$this->load->view('template', $data);
}
}
$this->agent_model->update($_POST, $id);
alert('Update', $_POST['agent_name']);
redirect('agent');
}
}
Model:
public function update($data, $id) {
$updatedata = array(
'name' => $data['agent_name'],
'mobile' => $data['mobile'],
'password' => sha1($data['password']),
'vehicle' => $data['agent_vehicle'],
'address' => $data['agent_address'],
'category' => $data['category'],
'created_on' => date('Y-m-d h:i:sa')
);
if (!empty($data['agent_img_url'])) {
$updatedata['img_url'] = $data['agent_img_url'];
}
$this->db->where('id', $id);
$this->db->update('agent', $updatedata);
}
View:
<div class="form-group">
<img src="/<?= $result['img_url']; ?>" class="img-responsive" name="old_agent_image" width="133" height="100">
</div>
<div class="form-group">
<label>Agent Image</label>
<input type="file" name="agent_image">
</div>
MY question: I edit image for particular user then image uploaded,but if image size more then 1mb ,then image can not upload and display error message.
so my question how to show errormsg.
$uploaded = $this->upload->do_upload('file'); //'file' is input field name
if($uploaded) {
$upload_data = $this->upload->data();
// do database stuff
} else {
$data['errors'] = array("error" => $this->upload->display_errors());
}
upload file all data wrok properly but file upload not save in server also file name not get
view
<form class="form-horizontal" role="form" method="post" action="<?php echo base_url('main/sent'); ?>" id="frm1">
<input type="file" name="userfile">
</form>
controller main
inside
public function sent(){
$config = array(
'upload_path' => "./inquery/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
var_dump($data);
}
Make sure your form is able to submit files:
<form enctype="multipart/form-data" ...>
Give the enctype attribute as above and you must be done!
And let the function know what field in the form your file comes from:
if($this->upload->do_upload('userfile'))
...
When you are uploading a file you need to set the enctype property of form element to enctype="multipart/form-data"
and i have appended a else clause to display error if upload is not successful. So you can figure out the reason!!!
public function sent(){
$config = array(
'upload_path' => "./inquery/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
var_dump($data);
}else{
print_r($this->upload->display_errors()));
}
I am working on a project where volunteers have to fill in registration form.
I want volunteers to be able to upload a profile image and save image name to database as well as the post data on the same form.
How can I achieve that?
In the success part of upload form could do a if statement like below for update
Update
$user_info = $this->get_user();
if ($user_info) {
$image_info = $this->upload->data();
$data = array(
'username' => $user_info['username'],
'image' => $image_info['file_name']
);
// Only updates password if post value true
if(isset($_POST['password'])) {
$data = array(
'password' => $this->input->post('password');
);
}
$this->db->where('id', $user_info['id']);
$this->db->update('tablename', $data);
}
Else In the success part of upload form could do insert like below
Insert
$image_info = $this->upload->data();
$data = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
'image' => $image_info['file_name']
);
$this->db->insert('tablename', $data);
Upload Controller
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0'; // Unlimited
$config['max_width'] = '0'; // Unlimited
$config['max_height'] = '0'; // Unlimited
$this->load->library('upload', $config);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);
$input_name = "userfile";
if ( ! $this->upload->do_upload($input_name))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$user_info = $this->get_user();
if ($user_info) {
$image_info = $this->upload_data();
$data = array(
'username' => $user_info['username'],
'image' => $image_info['file_name']
);
$this->db->where('id', $user_info['id']);
$this->db->update('tablename', $data);
}
$this->load->view('upload_success', $data);
}
}
public function get_user() {
// your user model data
}
}
?>
View
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="text" name="name"/>
<input type="password" name="password"/>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Upload library
Codeigniter 2 http://www.codeigniter.com/userguide2/libraries/file_uploading.html
Codeigniter 3 http://www.codeigniter.com/user_guide/libraries/file_uploading.html
your question is not clear ,
provide ,view ,controller as well.
anyways see this post it will help you.
click here
or
use <?php echo form_open_multipart('yourcontroler/add');?>
in your view file when dealing with image and text data.
in controller ,the best practice is to define two function one for image upload the other to validate ,and filter data.Like
public function do_upload() {
$path = '_assets/images/';
$fileName = 'userpic.png';
$config = array(
'allowed_types' => 'jpg|png|gif',
'upload_path' => $path,
'max_size' => '200',
'overwrite' => true,
'file_name' => $fileName,
'max_height' => "300",
'max_width' => "300",
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('logo')) {
echo "error ";
die;
} else {
$imageData = $this->upload->data();
}
}
and for data storing use simple function you usually do with form input.
I have a form am submitting and user has the ability to upload a picture in that form then submit the form as whole. I found a tutorial on codeigniter site showing upload form (dedicated only to upload, not other data along). link is: Codeigniter Upload Tutorial. How can I submit the form and then upload the files while also uploading other details to other table in database?
Below is an example
function add_staff(){
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
// field name, error message, validation rules
$this->form_validation->set_rules('name', 'Full name', 'trim|required');
$this->form_validation->set_rules('designation', 'Last Name', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$data['main_content'] = 'staff/add_staff';
$this->load->view('admin/template',$data);
}
else
{
$config['upload_path'] = 'uploads/staff/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('file'))
{
$data['error'] = array('error' => $this->upload->display_errors());
$new_staff = array(
'name' => $this->input->post('name'),
'designation' => $this->input->post('designation'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'description' => $this->input->post('description'),
'status' => $this->input->post('status')
);
}
else
{
$file_data = $this->upload->data('file');
$new_staff = array(
'name' => $this->input->post('name'),
'designation' => $this->input->post('designation'),
'phone' => $this->input->post('phone'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'photo' => $file_data['file_name'],
'description' => $this->input->post('description'),
'status' => $this->input->post('status')
);
}
if($this->staff_model->add_staff($new_staff)):
$this->session->set_flashdata('success', 'Staff added Sucessfully !!');
endif;
redirect('admin/staff');
}
}
you can pass in other data as well along with file form your form to controller, like
<?php echo form_open_multipart('upload/do_upload');?>
<input type="text" name="someField" value="" />
<input type="file" name="userfile" size="20" />
<input type="submit" value="upload" />
</form>
and in your upload controller's do_upload function:
$someField = $this->input->post("somefield"); //save to some db
//and rest of your file to be uploaded code from the same link you provided
Did you mean something like this
Image MOO is a excellent library that can accomplish image manipulations on the fly....
e.g for resizing the uploaded image to a particular path, you just have to do this
public function thumbnailer($uploader_response,$field_info,$files_to_upload)
{
$this->load->library('image_moo');
$file_uploaded=$field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnails=$field_info->upload_path.'/thumbnails/'.$uploader_response[0]->name;
$cart_thumbnails=$field_info->upload_path.'/cart_thumbnails/'.$uploader_response[0]->name;
$this->image_moo->load($file_uploaded)->resize(400,250)->save($thumbnails,false);
}
Hope this helps !!