I want to have a multiple file upload code.
For example:
Koala.jpg
Penguins.jpg
Jellyfish.jpg
There is input text where the user can set the new name of the image.
The user will now upload the images and the inputted text for new image name is "Animals"
Now, what I want is when this uploaded the output should be Animals1.jpg, Animals2.jpg, Animals3.jpg.
The problem is when I tried to upload all these images, only one image is uploading.
I tried to make research and applied some codes on my program, but still not working.
Controller
public function do_upload() {
$config = array(
'image_library' => 'gd2',
'file_name' => $this->input->post('finame'),
'upload_path' => './public/img/uploads',
'upload_url' => base_url().'public/img/uploads',
'allowed_types' => 'gif|jpg|jpeg',
'max_size' => '1024KB',
'max_width' => '1024',
'max_height' => '768',
'maintain_ratio'=> TRUE,
'overwrite' => false,
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error_msg = "<div class='alert alert-error'>".$this->upload->display_errors()."</div>";
$error = array('error' => $error_msg);
}
else {
$upload_data = $this->upload->data();
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
'image' => $data['thumbnail_name'],
'image_name' => $upload_data['file_name'],
//'description' => "",
'date_created' => date('Y-m-d H:i:s', now()),
'date_modified' => date('Y-m-d H:i:s', now()),
'author' => $this->session->userdata('username'),
'size' => $upload_data['file_size'],
'type' => $upload_data['image_type'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
//'document_name' => $field,
//'department' => $field2,
//'notes' => "",
);
$this->session->set_userdata('image_print', $file_array);
$this->load->database();
$this->db->insert('tbl_image', $file_array);
$data = array('upload_data' => $this->upload->data());
$user_level['records']=$this->user_model->get_records();
$this->load->view('v_dashboard/page/header_view', $user_level);
$this->load->view('v_document/upload/upload_result_view', $data);
$this->load->view('v_dashboard/page/footer_view');
}
}
I have this on my HTML
<label for="file"><strong>Select File To Upload:</strong></label>
<input type="file" name="userfile[]" multiple class="btn transcolor btn-file"/>
<br/><br/>
By the way, I'm using BLOB on my database.
I tried to refer to this links
Ellislab
GitHub
StackOverflow
StackOverflow
CodingLikeASir
You have to run the for loop till the count of the uploaded image file.
Like this:
for ($i=0; $i < count($_FILES['userfile']['name']); $i++)
{
// function to add the image name one by one into database.
}
Related
A Problem was encountered while attempting to move the uploaded file to the final destination
Unable to upload the file in codeigniter using their upload class, upload directory is also exist,
below is a code
$config = array(
'upload_path' => './upload_dir/users_docs/',
'allowed_types' => 'gif|png|jpeg',
'max_size' => 0,
'max_width' => 0,
'max_height' => 0,
'file_name' => $this->currTime,
);
$this->load->library('upload', $config);
if (!empty($_FILES['company_logo']['name']) && !$this->upload->do_upload('company_logo')) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error_message', $error['error']);
redirect('home');
}
$data = array('upload_data' => $this->upload->data());
There was an issue in the 'file_name' => $this->currTime, the format of time was not correct, Apostrophe where coming in the filename var, I just changed it to something else, now it's working, Thank you
I have tried all solutions but I can't tell what's wrong. Codeigniter keeps telling me that there's no file uploaded. I created the folder at the root of the project. I've seen other similar questions but I can't manage to make it work with their solutions.
This is my controller:
public function index() {
$this->form_validation->set_rules('name', 'name', 'required|trim|max_length[45]');
$this->form_validation->set_rules('type_id', 'type', 'required|trim|max_length[11]');
$this->form_validation->set_rules('stock', 'stock', 'required|trim|is_numeric|max_length[11]');
$this->form_validation->set_rules('price', 'price', 'required|trim|is_numeric');
$this->form_validation->set_rules('code', 'code', 'required|trim|max_length[45]');
$this->form_validation->set_rules('description', 'description', 'required|trim|max_length[45]');
$this->form_validation->set_rules('active', 'active', 'required|trim|max_length[45]');
$this->form_validation->set_rules('unit_id', 'unit', 'required|trim|max_length[45]');
$this->form_validation->set_rules('userfile', 'File', 'trim');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) { // validation hasn't been passed
$this->load->view('product/add_view');
} else { // passed validation proceed to post success logic
// build array for the model
$form_data = array(
'name' => set_value('name'),
'type_id' => set_value('type_id'),
'stock' => set_value('stock'),
'price' => set_value('price'),
'code' => set_value('code'),
'description' => set_value('description'),
'active' => set_value('active'),
'unit_id' => set_value('unit_id')
);
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
$this->upload->initialize($config); //Make this line must be here.
$imagen = set_value('userfile');
// run insert model to write data to db
if ($this->product_model->product_insert($form_data) == TRUE) { // the information has therefore been successfully saved in the db
if (!$this->upload->do_upload($imagen)) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('product/add_view', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('product/add_view', $data);
}
} else {
redirect('products/AddProduct', 'refresh');
// Or whatever error handling is necessary
}
}
}
This is my view (just showing the part that matters)
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => '');
echo form_open_multipart('products/AddProduct', $attributes); ?>
<p>
<label for="picture">Picture <span class="required">*</span></label>
<?php echo form_error('userfile'); ?>
<?php echo form_upload('userfile')?>
<br/>
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
EDIT: Applying the modification from the answer I get an error 500.
I tried to work on your code. and it worked on me after this changes
$config = array(
'upload_path' => "./uploads/",
'upload_url' => base_url()."uploads/", // base_url()."/uploads/", //added
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload'); //changed
$this->upload->initialize($config); //Make this line must be here.
$imagen = userfile; // $_FILES['userfile']['name'] //changed
If you get The localhost page isn’t working localhost is currently unable to handle this request. HTTP ERROR 500 possible error on your syntax, I just add 1 more } at the end of your code. You can check phpinfo() for other information. Also, there might be a problem on your file locations maybe the address you specified or the permissions if your running this on server.
I am working on a codeigniter project where users can create profiles and upload their images..I want to create an image thumbnail (200 X 200) and save it in the directory. My code works perfectly as it uploads the image and also stores the image name in database but the problem is it does not resize the image..I want to resize all images (200 X 200) and then store them in directory.
This is controller
public function FunctionName()
{
$fname = ucwords($this->input->post('fname'));
$lname = ucwords($this->input->post('lname'));
$prof_pic = $_FILES['profile_pic']['name'];
$config = array ('upload_path' => './images/students/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE,
'file_name' => $Maxtype."_".$fname."_".$lname,
'remove_spaces' => TRUE,
'image_library' => gd2,
'source_image' => $prof_pic,
'new_image' => './images/students/',
'maintain_ratio' => TRUE,
'height' => 200,
'width' => 200
);
$this->load->library('upload', $config);
$this->upload->do_upload('profile_pic');
$extension = pathinfo($prof_pic);
$ext = $extension['extension'];
$image = $config['file_name'].".".$ext;
$data = array('std_fname' => $fname,
'std_lname' => $lname,
'profile_pic' => $image
);
}
$config['allowed_types'] = 'doc|Doc';
I want to upload Doc format file only but cant upload doc format file just written The filetype you are attempting to upload is not allowed.
Why?
How to solve issue,
I add mimes file,
'doc' => 'application/msword',
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
you can use this function
public function do_upload(){
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
'upload_url' => base_url()."files/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
$this->remove_dir($this->config["upload_path"], false);
$this->ci->load->library('upload', $this->config);
if($this->ci->upload->do_upload())
{
$this->ci->data['status']->message = "File Uploaded Successfully";
$this->ci->data['status']->success = TRUE;
$this->ci->data["uploaded_file"] = $this->ci->upload->data();
}
else
{
$this->ci->data['status']->message = $this->ci->upload->display_errors();
$this->ci->data['status']->success = FALSE;
}
}
Ok try to upload file for hours but i get error,
You did not select a file to upload.
my code is in CI
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/uploads/",
'upload_url' => base_url()."uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $this->config);
if($this->upload->do_upload('logo'))
{
echo "file upload success";
}
else
{
echo $this->upload->display_errors();
}
in view i have
<input type="file" name="logo"/>
when i print_r $_POST i get
Array ( [name_srpski] => tyre [name_english] => Client nametre [logo] => cipele-plava_1.jpg )
Where could be error its very important
Try the following in config:
'upload_path' => FCPATH . "/uploads/", // or use "./uploads/" instead
'max_size' => "1000", // remove the kb from the string. it requires only the number
Try changing $this->load->library('upload', $this->config); to
$this->load->library('upload');
$this->upload->initialize( $this->config );
Also the form type should be multipart
<form method="post" action="some_action" enctype="multipart/form-data" />