//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.
Related
I want to delete the old file from the folder ie, if I update an image the old one remains as it is in the folder and a new one is get uploaded.
Here updating is done fine but after frequent updating number of files is increasing .
This is the code for selecting the content from the database and also for updating
public function select_testimonials($id)
{
$this->session->set_userdata('id',$id );
$query = $this->cl_testimonials_model->select($id);
$data1['selectdata'] = $query->result();
// $this->index();
$query = $this->db->get("cl_testimonials");
$data1['records'] = $query->result();
$this->load->view('admin/testimonials/testimonials_edit',$data1);
}
public function update_testimonials()
{
$id = $this->input->post("id");
if ($this->input->post('submit')=="update")
{
if(!empty($_FILES['picture']['name']))
{
$config['upload_path'] = 'uploads/images/testimonials';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('picture'))
{
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
}
}
//Prepare array of user data
$data = array('name' => $this->input->post('name'),'content' => $this->input->post('content'),'picture' => $picture);
//Pass user data to model
$insertUserData = $this->cl_testimonials_model->update($id,$data);
}
//Storing insertion status message.
if(isset($insertUserData))
{
$data['message']="* testimonials updated successfully.";
}
else
{
$data['message']="* Some problems occured, please try again.";
}
$query = $this->db->get("cl_testimonials");
$data['records'] = $query->result();
$this->load->view('admin/testimonials/testimonials_listing',$data);
}
Fetch Existing testimonial picture path by id, update, then use unlink('old file path')
More here : Here
Use the unlink(); function in php. it is applicable also in Codeigniter.
unlink("target path/".$file name);
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().
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'] = '';
}
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 have a form which has 5 file input fields, all works fine with my CRUD methods if the file field is not empty, however the client now wants to set some of the fields to be non mandatory.
This is how I am trying to do it in my code, the problem I am encountering is declaring a null variable(to insert a blank value in the respective database field) if the file field is empty. I get an undefined variable message from codeigniter when nothing is uploaded...
This is the code that code checks if a file has been uploaded or not.
if(isset($_FILES['ticketing_summary_file']))
{
$this->upload->initialize($config);
if($this->upload->do_upload('ticketing_summary_file'))
{
$upload_data=$this->upload->data();
$ticketing_summary_file_name=$upload_data['file_name'];
$ticketing_summary_full_file_path = $path_to_uploads.'/'.$ticketing_summary_file_name;
$show['ticketing_summary_file_url'] = $ticketing_summary_full_file_path;
}
}
if(!isset($_FILES['ticketing_summary_file']))
{
$show['ticketing_summary_file_url'] = $empty_file_message;
}
Then this is how I insert the data into my database... I have tried declaring the contents of 'ticketing_summary_file' in the code below and above, but it says its empty either way
$show = array('tour_id' =>$tour,
'date' => $this->input->post('date'),
'location' => $location);
$id = $this->show_model->save($show);
Cheers for any help,
Dan
I ran into a similar problem. I ended up checking to see if the upload error matched the text in the lang file. There is probably a better way but it was a quick fix for me. I hope it helps.
<?php
//play code file field
$play_code_errors= '';
$data['play_code_file_name'] = uniqid('pc_', true) . 'csv';
$config['upload_path'] = './media/tmp/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '4024';
$config['file_name'] = $data['play_code_file_name'];
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('play_code_file'))
{
$play_code_errors = $this->upload->display_errors();
if($play_code_errors = $this->lang->line('upload_no_file_selected'))
{
$play_code_errors = '';
}
}