Codeigniter File Upload Custom Error Messages - php

I'm new to Codeigniter's Upload library, but I am familiar with the standard form library. My aim is to instead of use the standard upload library error messages (example: The file you are attempting to upload is larger than the permitted size.) use my own error messages.
In the standard form validation I could use the following code:
$this->form_validation->set_message('required', 'The %s is a required field. Please ensure this field contains the relevant data before continuing.');
However this method for the Upload Library doesn't seem to be the same as i'm attempting to instead of outputting the standard 'max_size' (upload form config rule) error message i want to use my own that actually includes the max size.
I was unable to find anything on this through Codeigniter's documentation, I will show you the code i am using in both my controller and view incase it is of any help:
controller:
$this->form_validation->set_rules('userfile', 'New Image', 'trim|callback_valid_upload');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$data['upload_data'] = '';
if (!$this->upload->do_upload('userfile')) {
$this->data['error'] = $this->upload->display_errors('<p>', '</p>');
} else { //else, set the success message
$this->data['success'] = "Upload success!";
$this->data['upload_data'] = $this->upload->data();
}
view (to output errors):
if (isset($upload_data)) {
foreach($upload_data as $key => $value) { ?>
<li><?=$key?>: <?=$value?></li>
<?php
}
}
So in short where i have my config items such as $config['max_size'] = '1'; in my controller i would like to set and error message for it and this doesn't seem to work: $this->form_validation->set_message('max_size', 'The file you have uploaded exceeds the %s MB file size.');
Thanks

You could try and example some thing like this for custom error messages.
if(file_exists(dirname(FCPATH) . '/install/index.php')) {
$data['error_install'] = $this->lang->line('error_install');
} else {
$data['error_install'] = '';
}

Related

how to fix unwanted double copy of uploaded file?

My code is running fine and adding the files correctly but it is adding one additional copy of uploaded file. What is wrong?
My controller is like this:
public function add_audio(){
$config['upload_path'] = './musics/';
$config['allowed_types'] = 'mp3';
$config['max_size'] = '999999999999999999999';
$this->load->library('upload',$config);
$this->upload->do_upload();
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
print_r($data['error']);
//line of codes that displays if there are errors
}
else
{
$data['audio'] = $_FILES['userfile']['name'];
$this->load->model('main');
$query = $this->main->insert('audio',$data);
if($query == TRUE){
$this->load->view('admin/success');
}
}
... but it is adding one additional copy of uploaded file. What is wrong?
That's because you're calling do_upload() twice...
public function add_audio()
{
....
$this->upload->do_upload(); // <- FIRST upload (remove this one)
if ( ! $this->upload->do_upload() ) // <- SECOND upload
{
....
Since you may need the conditional logic that handles upload failures, delete the first instance of do_upload().

file name displayed same for all fields, using codeigniter's upload library

I have a view file, which includes form:
form_upload('media_image_00');
form_upload('media_image_01');
.....
form_upload('media_image_14');
And my controller(I have set the validation rules in model):
if ($this->form_validation->run() == TRUE) {
$config['upload_path'] = './my_image/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '10240';
$this->load->library('upload', $config);
for($i = 0;$i<=14;$i++){
$name = 'media_image_0'.$i;
if($i>9){
$name = 'media_image_'.$i;
} // end of if($i>9
if(!empty($_FILES[$name])){
$this->upload->do_upload($name);
$upload_data = $this->upload->data();
$_POST[$name] = $upload_data['file_name'];
dump($upload_data['file_name']);
}else{
// do nothing
}// end of if(!empty
} // end of for loop
I want the dump($upload_data['file_name']) to dump the filename, only where the datas are selected & uploaded. Not in every field.
The problem is when I upload data only on media_image_00 & media_image_01, it automatically dumps the name of media_image_01 to every other field after it. File is not uploaded again but the name is same for every field after media_image_01.
I don't know what the problem is. May be it is something to do with array in codeigniter.
How can I solve this?

Attachement file deletion after sending out an email

In a larger project, have a form which uploads 2 files. I am using codeigniter as the framework. After the form upload it should send these 2 files as an email.
In order to attach it to an email, we should have a local copy of the file. Hence I move the files to a temporary folder and use the naming convention of [session-id]_my_file_1 and [session-id]_my_file_2
Finally after sending out the email I try to delete the these temporary files. But unlink is not deleting these files. I donot know the reason for this.
My guess is: It may be still being used by the mail command to upload/send. Below are the code outline I have written.
$config['upload_path'] = './tmp_holder/';
$config['allowed_types'] = 'doc|docx|pdf|rtf';
$config['max_size'] = '10240';
$config['file_name'] = $this->session->userdata('session_id').'_1';
$config['overwrite'] = TRUE;
$config2['upload_path'] = './tmp_holder/';
$config2['allowed_types'] = 'doc|docx|pdf|rtf';
$config2['max_size'] = '10240';
$config2['file_name'] = $this->session->userdata('session_id').'_2';
$config2['overwrite'] = TRUE;
$this->load->library('upload',$config);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('init_app_form');
}
else
{
if($this->upload->do_upload('cvFile') === FALSE) {
$this->load->view('init_app_form');
}
else {
$file1Return = $this->upload->data();
$this->upload->initialize($config2);
if($this->upload->do_upload('researchFile') === FALSE) {
$this->load->view('init_app_form');
}
else {
//process data here
$file2Return = $this->upload->data();
$this->config->load('email');
$this->email->initialize($this->config->item('email_conf'));
$this->email->from($this->config->item('email_from'), $this->input->post('tname').' '.$this->input->post('fname').' '.$this->input->post('lname'));
$this->email->to($this->config->item('email_to'));
$this->email->subject('something');
$this->email->message('something');
$this->email->attach($file1Return['full_path']);
$this->email->attach($file2Return['full_path']);
if( $this->email->send() == false ) {
//error
echo $this->email->print_debugger(); exit;
}
$this->email->clear();
////////////////////////////////
#unlink($this->session->userdata('session_id').'_2');
////////////////////////////////
$this->load->view('init_app_success');
}
////////////////////////////////
#unlink($this->session->userdata('session_id').'_1');
////////////////////////////////
}
}
The Solution I did was to delete the files in the temp folder before I do this so that previous files are cleared. But this is not a clean approach right? This is because:
It may be trying deleting other files currently being used by other parallel instances
I want the tmp folder to be empty after I send itself.
Its better to use file_helper which is in code igniter.
$this->load->helper("file");
delete_files($this->session->userdata('session_id').'_1');
here is documentation for this

data not setting in session codeigniter

//uploading product movie or image?
if($this->input->post('upload_360') == "Upload") {
$config['upload_path'] = './media/images/products/360s';
$config['allowed_types'] = 'swf';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('film')) {
$this->data['product_error'] = $this->upload->display_errors();
$this->template->build('/admin/products/create', $this->data);
} else {
$this->data['data_360'] = $this->upload->data();
$this->session->set_userdata(array('360_film' => $this->data['data_360']));
$this->template->build('/admin/products/create', $this->data);
}
$this->session->set_userdata(array('advantages' => $this->input->post('product_advantages')));
$this->data['session_advantages'] = $this->session->userdata('advantages');
}
//upload the product image, if successful the user will be
//notified if the image is too high or wide, and will be offered,
//the chance to crop the image. All cropping takes place in the media
//controller.
if($this->input->post('product_image') == "Upload") {
$config['upload_path'] = './media/images/products/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('image_upload')) {
//die("!");
$this->data['image_error'] = $this->upload->display_errors();
$this->template->build('/admin/products/create', $this->data);
} else {
$this->data['image_data'] = $this->upload->data();
$this->session->set_userdata(array('image' => $this->data['image_data']));
$this->data['session_image'] = $this->session->userdata('image');
$this->template->build('/admin/products/create', $this->data);
}
$this->session->set_userdata(array('advantages' => $this->input->post('product_advantages')));
$this->data['session_advantages'] = $this->session->userdata('advantages');
}
if($this->input->post('screenshot_upload') == "Upload") {
$config['upload_path'] = './media/images/products/360s/screenshots/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('screenshot')) {
//die("!");
$this->data['screenshot_error'] = $this->upload->display_errors();
$this->template->build('/admin/products/create', $this->data);
} else {
$this->data['screenshot_data'] = $this->upload->data();
$this->session->set_userdata(array('screenshot' => $this->data['screenshot_data']));
$this->data['session_screenshot'] = $this->session->userdata('screenshot');
$this->template->build('/admin/products/create', $this->data);
}
$this->session->set_userdata(array('advantages' => $this->input->post('product_advantages')));
$this->data['session_advantages'] = $this->session->userdata('advantages');
}
On my form I have the user choose a file and the click an upload button dependent on which button is clicked the file gets uploaded and the upload data gets saved in a session.
The session is then used to get data from to save to a database, the upload_360 session works, the product_image session works fine but the screenshot_upload session only has data when with the if statement (3rd one in the code) if I try and acccess it outside of the code then that portion of the session is empty?
Is there a reason for this?
Why are you storing data in a session before inserting it into the db?
Cookies can only hold 4KB of data...
but the screenshot_upload session only has data when with the if statement (3rd one in the code) if I try and acccess it outside of the code then that portion of the session is empty?
I don't understand that part of your question. Do you mean it only has data when only using the 3rd if statement? i.e. when only trying to do screenshot_upload and not product_image or 360_upload`? If so, that might speak to the cookie size limit.
Instead of
$this->session->set_userdata(array('screenshot' => $this->data['screenshot_data']));
$this->data['session_screenshot'] = $this->session->userdata('screenshot');
why don't you
$this->uploads_model->insert_screenshot_data($this->data['screenshot_data']);//send screenshot upload_data to model to be inserted into db
$this->data['screenshot_data'] = $this->data['screenshot_data'];//if you want to pass screenshot upload_data to template/view
?
It looks like you are sending output to the user before setting the session (I'm inferring this from the $this->template->build, which is custom code.)
The session, like headers, cannot be modified after anything (ANYTHING) has been sent to output. This is because the session itself is sent in the header.

why this error happens with codeigniter fileupload?

this is my upload function
public function do_upload()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('customer_photo'))
{
$responce->success = false;
$responce->data['error'] = $this->upload->display_errors();
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->_do_resize($data);
}
echo json_encode($responce);
}
this is the json what im seeing on firebug console
{"success":false,"data":{"error":"<p>The filetype you are attempting to upload is not allowed.<\/p>"}}</p>
any idea why its contain </p> and these <\/p> ?
Regards
According to the manual, $this->upload->display_errors() wraps the error messages in <p> tags.
You can pass parameters for the delimiters, to wrap the errors in what you want.
$this->upload->display_errors('', '');

Categories