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().
Related
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?
I am trying to upload two files with the help of codeigniter functions.
One of the file should be mandatory and one should be optional.
I am using the code below to upload those files but I cannot figure out the way to make one optional and one mandatory. I tried few modifications to the code below, but i bumped into many errors. I am new to codeigniter.
Even the code below for handling the uploads may not be appropriate but it is working.
$config['upload_path'] = 'uploads/';
$path=$config['upload_path'];
$config['allowed_types'] = '*';
$this->load->library('upload');
$i=0;
foreach ($_FILES as $key => $value)
{
if (!empty($key['name']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($key))
{
$errors = $this->upload->display_errors();
$this->session->set_flashdata('error', $errors);
redirect(base_url().'upload', 'refresh');
}
else
{
$data = array('upload_data' => $this->upload->data());
$p[$i] = $this->upload->data();
}
}
$i++;
} //endforeach
if(empty($errors)){
//if there are no errors, write it into the database
$data = array('user_id'=>$this->session->userdata('id'),
'name'=>$this->input->post('name'),
'screenshot'=>$p[1]['file_name'],
'model'=> $p[0]['file_name'],
'created'=>date('Y-m-d H:i:s')
);
if($this->usermodel_model->save($data)){
//success
redirect(base_url().'dashboard?success');
}else{
//failed
redirect(base_url().'upload');
}
}
Sohanmax put $i++; inside if (!empty($key['name'])){ } and after ending foreach check if($i !=0) if it's false show the error, hope it'll work.
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
//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.
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('', '');