codeigniter file upload - php

I am doing the following to upload a file(image) using codeigniter. What I am wanting to do is modify the error message so that it obvious which field the error relates too as there are multiple upload options on the page, below is my upload code,
$config['upload_path'] = './media/uploads/users';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '90';
$config['max_height'] = '60';
$this->upload->initialize($config);
if(!$this->upload->do_upload('logo_small'))
{
$error = array('error' => $this->upload->display_errors());
$this->template->build('users/employer', $error);
}
else
{
$file = $this->upload->data();
$logo_small = "small_".$file['file_name'];
}
So basically I want there error message to state Logo Small in the error message, and then if the error relates to Logo large etc I would want it to state that.

easiest way is to edit system/language/english/upload_lang.php with the error message you want.

Related

Unable to upload these formate file in codeigniter STEP, STP, IGES, IGS, SLDPRT, 3DM, SAT or X_T

Unable to upload these formate file in codeigniter STEP, STP, IGES, IGS, SLDPRT, 3DM, SAT or X_T
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf|step';
$config['max_size'] = 100000000000;
//$config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('fileUpload'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
// echo 'error';
// $this->load->view('upload_form', $error);
}else{
$this->upload->data();
}
error :- The filetype you are attempting to upload is not allowed
for those file types, just change your config of allowed_types into this to override checking extension:
$config['allowed_types'] = "*";
Before uploading your file, codeigniter check your file's extension first, but your file types are not in application/config/mimes.php so codeigniter does not know and then always return FALSE.
Another way is to add your mimes type into mimes.php, you can check out this in this link

Codeigniter Upload Image Choice

I have a form including different inputs and file upload input. I want the users if they want to upload images then upload, but if they don't want to upload. Don't give an error.
if($_POST){
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
//Sending this $error to view and if a user didnt upload image and just filled
//the other inputs it shows error. but i dont want that.
} else { }
} else {
redirect(site_url());
}
You are on right track. Just delete or comment this line
$error = array('error' => $this->upload->display_errors());
You might be send your $error to view file like below:
$this->load->view('upload_form', $error);
So Don't send your value to view file. Just call your view when image successfully uploaded below:
if ( ! $this->upload->do_upload('userfile') )
{
// $error = array('error' => $this->upload->display_errors());
/*Delete or comment this line. Do your other stuff here if image not uploaded.*/
}else{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
/* This is example. You can do anything on successfully image upload.*/
}
You can refer codeigniter manual https://www.codeigniter.com/userguide3/libraries/file_uploading.html
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
//$rand_str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// $filename=md5(str_shuffle($rand_str.mt_rand()));
// $config['file_name']=$filename;
$this->upload->initialize($config);
if ( $this->upload->do_upload('userfile'))
{
//$data = array('upload_data' => $this->upload->data());
// $data["filename"]=$filename;
// $data["ad_id"]=$lid;
// $data["filename"]=$rand_name;
// $this->Ads_model->insert_image($data);
}
I solved my problem changing the Upload class to give no error. I commented the lines including "no selected files".

Getting attachments from http post request in Codeigniter

I'm using Mailgun to receive emails for my web app developed using Codeigniter. Mailgun makes a http post to the following (http://something.com/email) url for every incoming email.
I get the contents of the email with the following code.
function email_to_db(){
$email_body = $this->input->post('body-plain', '');
$email_sender = $this->input->post('sender');
}
However I'm not able to fetch the attachments.
Can someone guide me on how I can achieve this. Following is the link to my reference doc.
attachments are files, you should use File Uploading Class
$config = [];
$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());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
you can also do print_r($_FILES); and check the attachment files.

check allowed file type while uploading in codeigniter

I don't want force my web admin to upload image while adding product through dashboard, but if he/she uploads then i want to check for the allowed file type and if not allowed i want to throw error message in codeigniter.
here is what i did according to user guide of codeigniter:
$config['upload_path'] = './content/uploads/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2000';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->form_validation->set_rules('pName', 'Name', 'required|xss_clean|max_length[200]');
$this->form_validation->set_rules('pPrice', 'Price', 'required|xss_clean|max_length[200]');
if (($this->form_validation->run() == FALSE) || (!$this->upload->do_upload())) {
$data['error'] = $this->upload->display_errors();
$this->load->view('product/addProduct', $data);
} else {
//if valid
if ($this->upload->do_upload('myfile')) {
$data = array('upload_data' => $this->upload->data('myfile'));
$productImg = $data['upload_data']['file_name'];
but this method forces me to upload image, which i want optional. So is there any method/function or way to check is the file uploaded is of allowed format on upload and leave unchecked if not uploaded.
Thanks in advance

Image Type Validation in codeigniter

I am new to Codeigniter. Working on an form validation having image in the form. Using Module MVC extension in CodeIgniter.
//for upooading image I found this file upload snippet
$config['upload_path'] = './uploads/audio_files/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($file_name))
{
$error = array('error' => $this->upload->display_errors());
$file1 = '';
}
else
{
$upload_data = $this->upload->data();
$file1 = $upload_data['file_name'];
}
This is working fine. File which is not jpeg | gif is not uploaded. but user dont get any error message. I want the user to show that particular error when other image type is uploaded. Please help
I tried this but failed.
try 1 : $this->template->load_partial('admin/admin_master', 'admin/form', $error);
try 2. : loading view with $error
Thanks
Can't you associate a view with an error message? For instance:
After
$file1 = '';
You put:
$this->load->view('error_view', $error, $file1);
You would have to make a separate view page called 'error_view.php'.

Categories