File not uploading in codeigniter - php

This is the codeigniter function for file uploading
public function doctor_signup()
{
$this->load->library('encrypt');
$rand = time() . rand(0, 9999);
if($_FILES["file"]["name"]!="")
{
$config['upload_path'] = realpath(dirname(__FILE__)). '/uploads/';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['name']=$rand;
print_r($config);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
echo $error = array('error' => $this->upload->display_errors());
exit;
}
}
$data = array(
'username' => $_POST['username'],
'password' => $this->encrypt->encode($_POST['password']),
'name' => $_POST['name'],
'address' => $_POST['address'],
'city' => $_POST['city'],
'state' => $_POST['state'],
'photo'=> $rand,
'email' => $_POST['email'],
'date_of_join'=>date('Y-m-d H:m:s'),
'landline' => $_POST['landline'],
'mobile' => $_POST['mobile'],
'specialist' => $_POST['specialist'],
'comments' => $_POST['comments'],
'degree' => $_POST['degree']
);
if( $this->db->insert('doctor_user', $data))
{
$this->load->view('header', $data);
$this->load->view('user_created', $data);
$this->load->view('footer', $data);
}
}
But the file is not uploaded to the upload directory an also not giving any error. The uploads folder in under the home directory. Any suggestion.
Thanks

I think it could be because $config['name'] should be
$config['file_name']
Try renaming this and see if it works. Also you are not using $config['allowed_types'] to specify what files can be uploaded. Otherwise whats to stop someone upload a nasty file?

Related

Codeigniter image name send to data base as name

is this thing do is possible?
I want to send data and image name to database please help me to fix this error
i this image text name not send to db thats why there was a error please help me to fix this error
this is my controller
public function upload_file()
{
$config['allowed_types'] = '*';
$config['file_name'] = $data-> 'filename';
$config['upload_path'] = './uploads/Ehi';
$config['encrypt_name'] = false;
$this->load->library('upload', $config);
if ($this->upload->do_upload('image'))
{
print_r($this->upload->data());
$this->load->model('ehi_model');
$data = array(
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'activedays' => $this->input->post('activedays'),
'filename' => $data-> 'filename';
);
$this->ehi_model->upload_file($data);
redirect(base_url() ."Ehi/index");
}
else
{
print_r($this->upload->display_error());
}
}
and this is my model
function upload_file($data)
{
$this->db->insert('ehi', $data);
}
Where is this $data defined?
$config['file_name'] = $data-> 'filename';
But following change will work for you.
'filename' => $this->upload->data('file_name')
Reference
Use that.
$this->ehi_model->upload_file($this->upload->data('file_name'));

Codeigniter 3 and Ion-Auth application bug: undefined index userfile

I am working on a Social Network application with Codeigniter 3, Ion-Auth and Bootstrap 4. You can see the Github repo HERE.
I have tried to add an avatar at user's registration.
For this purpose, I first added an "avatar" column to the users table. Then, in the view I added:
<div class="form-group">
<?php $avatar['class'] = 'form-control';
echo lang('edit_user_avatar_label', 'avatar');?>
<input type="file" class="form-control" name="userfile" id="avatar" size="20">
</div>
In the Auth controller (application/controllers/Auth.php) I created this upload method:
public function upload_image() {
$config['upload_path'] = './assets/img/avatars';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = 2048;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->_render_page('auth' . DIRECTORY_SEPARATOR . 'create_user', $error);
} else {
$this->data = array('image_metadata' => $this->upload->data());
$this->_render_page('auth' . DIRECTORY_SEPARATOR . 'create_user', $this->data);
}
}
Finally, to the existing $additional_data array, from the orihinal create_user() method, I added the line 'avatar' => $_FILES['userfile']['name']:
$additional_data = [
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'avatar' => $_FILES['userfile']['name'],
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
];
The above line, when added to the $data array from the edit_user($id) method, has no errors, yet when added to the $additional_data array, it gives the error: Undefined index: userfile.
Where is my mistake?
UPDATE:
I replaced <?php echo form_open("auth/create_user");?> with <?php echo form_open_multipart("auth/create_user");?>.
Result: the image filename (with extension), is added in the users table avatar column. There is a problem though: the actual upload of the image, to ./assets/img/avatars does not take place.
Finaly working!!
if ($this->form_validation->run() === TRUE)
{
$email = strtolower($this->input->post('email'));
$identity = ($identity_column === 'email') ? $email : $this->input->post('identity');
$password = $this->input->post('password');
//return $this->upload_image();
$config['upload_path'] = './assets/img/avatars';
$config['file_ext_tolower'] = TRUE;
$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('userfile'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
$file_name = null;
}
else
{
$file_name = $this->upload->data('file_name');
}
$additional_data = [
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'avatar' => $file_name,
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
];
print_r($additional_data);
}
Result Array
Array ( [first_name] => admin [last_name] => admin [avatar] => design.png [company] => admin [phone] => 9999999999 )
The problem seems to be in your form. I reviewed your code and found that auth/create_user.php uses form_open() method instead of using form_open_multipart() method because a normal form won't post files, hence in your controller not getting userfile index from $additional_data variable.
UPDATE
In the comments OP posted a link to the full code. Checking that out, the problem is very clear. I described it, and a fix, in the comments below my answer. Copying that comment here:
You load the upload library on line 473, in the upload_image() method. But you are calling $this->upload->data() in a different method (line 530, in the create_user() method), where you have not loaded the upload library. Move the code from upload_image() into create_user(). Refactor once you have it working if you want, keep it simple until it is
Original Answer
It looks like you've been working through the documentation, your code is very similar to the example they provide. But you've stopped short of the critical last step where they explain how to access the details of the uploaded file! :-)
They demonstrate how to do that by returning a view with the upload data:
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
So the upload file info is available through $this->upload->data(), not PHP's superglobal $_FILES.
The docs go on to describe the data() method:
data([$index = NULL])
[...]
This is a helper method that returns an array containing all of the data related to the file you uploaded.
[...]
To return one element from the array:
$this->upload->data('file_name'); // Returns: mypic.jpg
So for your Ion Auth code, this should work (assuming the filename is all you need to store):
$additional_data = [
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'avatar' => $this->upload->data('file_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
];
As has been clearly explained in the other answers, here is the Copy and Paste answer for you.
To reiterate what has been already stated.
$this->upload->data('file_name'),
Does not exist as you have not performed the required steps to create it and hence why you were getting the very "clearly stated" error message.
So you need to add in...
$config['upload_path'] = './assets/img/avatars/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
So your code becomes...
if ($this->form_validation->run() === TRUE) {
$email = strtolower($this->input->post('email'));
$identity = ($identity_column === 'email') ? $email : $this->input->post('identity');
$password = $this->input->post('password');
//return $this->upload_image();
$config['upload_path'] = './assets/img/avatars/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$additional_data = [
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'avatar' => $this->upload->data('file_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
];
}
Now, as you have this in your do_upload() method, you could put the file upload code in another method and call it from both, so you are not "repeating yourself". I'll leave that up to you do work out.
Update: A possible refactoring
Create a new method to init the File Upload
protected function init_do_upload() {
$config['upload_path'] = './assets/img/avatars';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
}
Your existing do_upload() becomes...
/**
* Upload avatar
*/
public function do_upload() {
$this->init_do_upload();
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$this->data = array('upload_data' => $this->upload->data());
$this->_render_page('auth' . DIRECTORY_SEPARATOR . 'create_user', $this->data['upload_data']);
}
}
And the code segment in create_user() becomes...
if ($this->form_validation->run() === TRUE) {
$email = strtolower($this->input->post('email'));
$identity = ($identity_column === 'email') ? $email : $this->input->post('identity');
$password = $this->input->post('password');
$this->init_do_upload();
$this->upload->do_upload('userfile');
$additional_data = [
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'avatar' => $this->upload->data('file_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
];
}

How to solve error Number: 1048 Column 'photo_pict' cannot be null?

I got this error:
A Database Error Occurred
Error Number: 1048
Column 'photo_pict' cannot be null
INSERT INTO `tester` (`photo_pict`, `press_pict`, `name_pict`) VALUES (NULL, NULL, NULL)
Filename: C:/xampp/htdocs/pretest/system/database/DB_driver.php
Line Number: 691
I'm using CodeIgniter and I'm a beginner programmer.
I was making a register form, all I have to do is just saving the input from the register form to the database. But every data has been saved to database except the data for saving photo to the database.
Code show here
Model :
function insert($data){
$data = array(
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'birthDate' => $data['birthDate'],
'gender' => $data['gender'],
'posisi' => $data['posisi'],
'media_name' => $data['media_name'],
'media_region' => $data['media_region'],
'media_ctgry' => $data['media_ctgry'],
'company_addrss' => $data['company_addrss'],
'website' => $data['website'],
'editor_email' => $data['editor_email'],
'office_phone' => $data['office_phone'],
'office_fax' => $data['office_fax'],
'personal_email' => $data['personal_email'],
'phone_number' => $data['phone_number'],
'working_email' => $data['working_email']
);
$this->db->insert('tester', $data);
}
function upload($data){
$gambar = array(
'photo_pict'=> $data['photo_pict'],
'press_pict'=> $data['press_pict'],
'name_pict'=> $data['name_pict']
);
$this->db->insert('tester', $gambar);
}
function proses_regist(){
return $this->db->insert('tester', $data);
}
}
My Controller :
public function __construct(){
parent :: __construct();
$this->load->model("Model_regist");
$this->load->helper(array('url','form'));
}
public function index(){
$this->load->view('home');
}
function register(){
$gambar = array(
'press_pict'=>$this->upload->data("photo_pict"),
'photo_pict'=>$this->upload->data("press_pict"),
'name_pict'=>$this->upload->data("name_pict")
);
$data = array(
'first_name' => $this->input->post("first_name"),
'last_name' => $this->input->post("last_name"),
'birthDate' => $this->input->post("birthDate"),
'gender' => $this->input->post("gender"),
'posisi' => $this->input->post("posisi"),
'media_name' => $this->input->post("media_name"),
'media_region' => $this->input->post("media_region"),
'media_ctgry' => $this->input->post("media_ctgry"),
'company_addrss' => $this->input->post("company_addrss"),
'website' => $this->input->post("website"),
'editor_email' => $this->input->post("editor_email"),
'office_phone' => $this->input->post("office_phone"),
'office_fax' => $this->input->post("office_fax"),
'personal_email' => $this->input->post("personal_email"),
'phone_number' => $this->input->post("phone_number"),
'working_email' => $this->input->post("working_email")
);
$this->Model_regist->insert($data);
$this->Model_regist->upload($gambar);
$this->index();
}
private function uploadImage(){
$config['upload_path'] = './gambar/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['overwrite'] = true;
$nama_file = "gambar_".time();
$config['file_name'] = $nama_file;
$this->load->library('upload', $config);
if( ! $this->upload->do_upload('berkas')){
$error = array('error' => $this->upload->display_errors());
}else{
$this->upload->data();
}
$this->Model_regist->upload($gambar);
$this->Model_regist->insert($data);
$this->index();
}
I expect the photo that has been chosen can be saved in the database and can be saved in the file that I have made before. The file name is gambar.

Want to Remove Validation in Resume Upload in in codeigniter

We are working on Job Scrip. Faces problem in signup page validation. It has on Resume as well. We need to remove the Validation from Upload Resume. How to remove the Validation from the Signup page at upload Resume only. Please help us in this.
Controller is in codeigniter. Help us, we for stuck into this.
$this->form_validation->set_message('is_unique', 'The %s is already taken');
if (empty($_FILES['cv_file']['name']))
$this->form_validation->set_rules('cv_file', 'Resume', 'required');
$this->form_validation->set_error_delimiters('<div class="errowbox"><div class="erormsg">', '</div></div>');
if ($this->form_validation->run() === FALSE) {
$data['cpt_code'] = create_ml_captcha();
$this->load->view('jobseeker_signup_view',$data);
return;
}
$current_date = date("Y-m-d H:i:s");
$job_seeker_array = array(
'first_name' => $this->input->post('full_name'),
'email' => $this->input->post('email'),
'password' => $this->input->post('pass'),
'dob' => $this->input->post('dob_year').'-'.$this->input->post('dob_month').'-'.$this->input->post('dob_day'),
'mobile' => $this->input->post('mobile_number'),
'home_phone' => $this->input->post('phone'),
'present_address' => $this->input->post('current_address'),
'country' => $this->input->post('country'),
'city' => $this->input->post('city'),
'nationality' => $this->input->post('nationality'),
'gender' => $this->input->post('gender'),
'ip_address' => $this->input->ip_address(),
'dated' => $current_date
);
if (!empty($_FILES['cv_file']['name'])){
//$verification_code = md5(time());
$extention = get_file_extension($_FILES['cv_file']['name']);
$allowed_types = array('doc','docx','pdf','rtf','jpg','txt');
if(!in_array($extention,$allowed_types)){
$data['cpt_code'] = create_ml_captcha();
$data['msg'] = 'This file type is not allowed.';
$this->load->view('jobseeker_signup_view',$data);
return;
}
$seeker_id = $this->job_seekers_model->add_job_seekers($job_seeker_array);
$resume_array = array();
$real_path = realpath(APPPATH . '../public/uploads/candidate/resumes/');
$config['upload_path'] = $real_path;
$config['allowed_types'] = 'doc|docx|pdf|rtf|jpg|txt';
$config['overwrite'] = true;
$config['max_size'] = 6000;
$config['file_name'] = replace_string(' ','-',strtolower($this->input->post('full_name'))).'-'.$seeker_id;
$this->upload->initialize($config);
if (!$this->upload->do_upload('cv_file')){
$this->job_seekers_model->delete_job_seeker($seeker_id);
$data['cpt_code'] = create_ml_captcha();
$data['msg'] = $this->upload->display_errors();
$this->load->view('jobseeker_signup_view',$data);
return;
}
$resume = array('upload_data' => $this->upload->data());
$resume_file_name = $resume['upload_data']['file_name'];
$resume_array = array(
'seeker_ID' => $seeker_id,
'file_name' => $resume_file_name,
'dated' => $current_date,
'is_uploaded_resume' => 'yes'
);
}
Remove this
if (empty($_FILES['cv_file']['name']))
$this->form_validation->set_rules('cv_file', 'Resume', 'required');

codeigniter upload image

Hello all im working on a admin system that can create news with a image but i cant find out how to send the image name from my model file to my controller,
this is my model file:
function uploadImg()
{
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 2000,
'encrypt_name' => true
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 200,
'height' => 200,
'encrypt_name' => true,
'max_size' => 2000
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
# Ret profil billed navn #
$file_array = $this->upload->data('file_name');
return $billed_sti['billed_sti'] = $file_array['file_name'];
//$this->db->where('username', $this->input->post('username'));
//$this->db->update('users', $profilBilledNavn);
}
This is my controller:
function opret() {
$this->form_validation->set_rules('overskrift', 'overskrift', 'required');
$this->form_validation->set_rules('description', 'description', 'required');
$this->form_validation->set_rules('indhold', 'indhold', 'required');
if($this->form_validation->run() == true)
{
$this->load->model('admin/nyheder_model');
$billed_sti = $this->nyheder_model->uploadImg();
$data = array(
'overskrift' => $this->input->post('overskrift'),
'description' => $this->input->post('description'),
'indhold' => $this->input->post('indhold'),
'billed_sti' => $billed_sti,
'brugernavn' => $this->session->userdata('username'),
'godkendt' => 'ja'
);
$this->db->insert('nyheder', $data);
redirect('admin/nyheder/index');
} else {
$this->index();
}
}
I do the image processing in the controller rather than the model.
"Models are PHP classes that are designed to work with information in your database."
from: http://codeigniter.com/user_guide/general/models.html
What you need to do is move the code for uploading the image to the controler.
function do_upload()
{
$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('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Once you did that,
You can insert the name of the file from the $data variable created in this line:
$data = array('upload_data' => $this->upload->data());
and you can get the value like this:
$data['file_name']
The file will upload the the folder you configured, and you will insert the filename to the DB From the controller.
I hope it helps.
Please use the upload function in your controller as the model classes are used to handle the database information. Please check the code below
//Controller Class
function upload_image()
{
//Check for the submit
// Submit Name refers to the name attribute on the submit input tag.
// $filename refers to the name attribute of the file input tag.
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$submit = $this->input->post('submit');
if($submit == "Submit Name")
{
//Load the relevant classes and libraries
$this->load->library('upload');
$this->load->model('admin/nyheder_model','nmodel');
$filename = "image_file";
//Define the config array
$config = array();
$config['upload_path'] = $this->gallery_path;
$config['allowed_types'] = "jpg|gif|png";
$config['max_size'] = 0; //0 is for no limit
$this->upload->initalize($config);
if(!$this->upload->do_upload("$filename"))
{
echo $this->upload->display_errors();
}
else
{
$file_data = $this->upload->data();
$filename_1 = $file_data['file_name'];
$insert_array = array('filename'=>"$filename_1");
$this->nmodel->insert_data($insert_array);
} // end of the else statement
} // end of the isset statement
} // end of the outer conditional statement
Now you have the value of the filename in the $filename_1 variable which you can pass to the model class and can store the value in the database.
Thanks
J

Categories