I aam new to codeigniter, can any one tell me how do we can upload multiple files to the server at a same time with different names in codeigniter.
Create multiple file upload form. Here I refer to upload method in users controller.
<form method="post" action="<?php echo base_url('users/upload/'); ?>" enctype="multipart/form-data">
<label for="upload">Select : </label>
<input type="file" name="upload[]" id="upload" multiple="multiple" />
<input type="submit" name="fsubmit" id="fsubmit" value="Upload" />
</form>
And Write the following code in your upload method.
public function upload()
{
if (isset($_FILES['upload']['name'])) {
// total files //
$count = count($_FILES['upload']['name']);
// all uploads //
$uploads = $_FILES['upload'];
for ($i = 0; $i < $count; $i++) {
if ($uploads['error'][$i] == 0) {
move_uploaded_file($uploads['tmp_name'][$i], 'storage/' . $uploads['name'][$i]);
echo $uploads['name'][$i] . "\n";
}
}
}
}
The directory storage is example. You can move the files in your choice. NOTE It is not pure CodeIgniter method for multiple file uploads. Hope this helps thanks!!
you can upload files with different name in following way
in view
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile1" size="20" />
<input type="file" name="userfile2" size="20" />
in controller just pass field name to do_upload method
function do_upload()
{
$this->upload_file('userfile1');
$this->upload_file('userfile2');
$this->load->view('upload_form', $error);
}
function upload_file($field_name){
$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($field_name))
{
return array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
for more information please read CI user guide User guide
Related
I am trying to implement CodeIgniter's upload library. But everytime I tried to upload, it keeps on saying that I haven't selected a file to upload.
Here is the code:
View:
<form action="<?= base_url().'principal/updateuniform' ?>" method="post">
<h3 style="text-align:center">Polo</h3>
<input type="hidden" name="imageid" value="<?= $img_id ?>">
<input type="hidden" name="imagename" value="<?= $img_name ?>">
<input type="file" name="imagefile" accept="image/*" /><br>
<button type="submit" class="btn btn-gold"><span class="glyphicon glyphicon-upload" aria-hidden="true"></span> Upload</button>
</form>
Upload function:
function updateuniform(){
$img_id = $this->input->post('imageid');
$img_name = $this->input->post('imagename');
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 5000;
$config['max_width'] = 3000;
$config['max_height'] = 4000;
$config['overwrite'] = TRUE;
$config['file_name'] = $img_name;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$img = $this->input->post('imagefile');;
$id = $this->session->userdata('id');
$user['user'] = $this->UsersModel->select_principal($id);
if (!$this->upload->do_upload($img)){
$data['error'] = $this->upload->display_errors();
$data['uniforms'] = $this->InfoModel->getUniforms();
$this->load->view('include/header_principal',$user);
$this->load->view('principal/manage_uniforms', $data);
$this->load->view('include/footer_principal');
} else {
$img_path = 'images/'.$this->upload->data('orig_name');
$data['success'] = 'Uniforms has been updated.';
$this->InfoModel->updateUniform($img_path);
$this->load->view('include/header_principal',$user);
$this->load->view('principal/manage_uniforms', $data);
$this->load->view('include/footer_principal');
}
}
I also tried to echo the $img and it's displaying the file name.
$this->upload->do_upload($img)
The param should be the filedname,like:
$this->upload->do_upload('imagefile')
Add enctype="multipart/form-data" in your form tag of view this allows you to get $_FILE value and This will solve your issue I guess
I have a text form that works and I have a file uploading form that works using the exact example from the codeigniter docs, but I don't seem to be able to put the two together.
I want a form that allows text and file upload. The text should go to the database and the file to storage with filepath saved in the DB.
The Form you need:
<?php echo form_open_multipart('upload/do_upload');?>
Text: <input type="text" name="usertext" size="20" />
<br /><br />
File: <input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
<?php echo form_close();?>
in upload controller:
function do_upload()
{
$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('userfile'))
{
$error = array('error' => $this->upload->display_errors());
// upload error area
}
else
{
$data = array('upload_data' => $this->upload->data());
//upload success area
$userfile = $upload_data['file_name']; // the uploaded file name
$usertext = $this->input->post('usertext'); // the text from form
// Here you can insert the value of $usertext to db
}
}
anyone can help me.. its work only if at least 2 files uploaded and then the first file didn't move to upload folder.. anyone can help.. here's my code
// view
<?php echo form_open_multipart($this->uri->uri_string()); ?>
<p>
<?php echo form_label('Image') ?>
<input type="file" name="userfile[]" size="20" class="multi" accept="gif|jpg|png"/>
</p>
// controller
function album($id){
if (isset($_POST['submit']))
{
$config['upload_path'] = './assets/gallery/'; // server directory
$config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
$config['max_size'] = '1000'; // in kb
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('Multi_upload');
$files = $this->multi_upload->go_upload();
if ( ! $files )
{
$data['error'] = $this->upload->display_errors();
}
else
{
$data = array('upload_data' => $files);
}
}
$id = $this->uri->segment(3);
$data['query'] = $this->administrator_model->getAllPhoto($id);
$data['current'] = 'home';
$data['side'] = 'gallery';
$data['attr'] = 'view_album';
$data['content'] = 'backend/administrator_manage';
$data['sidebar'] = 'backend/home_sidebar';
$this->load->view("backend/index", $data);
}
// multi upload library
i use from here
with little modification
It is very simple to use pure PHP multi file Upload. Look into this script
View
<form method="post" action="<?php echo base_url('users/upload/'); ?>" enctype="multipart/form-data">
<label for="upload">Select : </label>
<input type="file" name="userfile[]" id="userfile" multiple="multiple" />
<input type="submit" name="fsubmit" id="fsubmit" value="Upload" />
</form>
Controller
public function album($id)
{
if (isset($_FILES['userfile']['name'])) {
// total files //
$count = count($_FILES['userfile']['name']);
// all uploads //
$uploads = $_FILES['userfile'];
for ($i = 0; $i < $count; $i++) {
if ($uploads['error'][$i] == 0) {
// FCPATH = root directory project
move_uploaded_file($uploads['tmp_name'][$i], FCPATH . 'assets/gallery/' . $uploads['name'][$i]);
echo $uploads['name'][$i] . "\n";
}
}
}
}
Hope this helps. Thanks!!
I would like to upload excel file many times in CodeIgniter, but not successfully
(The filetype you are attempting to upload is not allowed.)
Controller Coding:
$this->load->library('upload');
$this->load->helper('file');
//$config['upload_path'] = XLS_PATH;
$config['upload_path'] = './public/test/files/test/';
$config['allowed_types'] = 'xls|xlsx';
$config['max_size'] = 0;
$this->upload->initialize($config);
echo $this->upload->file_type;
if (!$this->upload->do_upload('userfile'))
{
echo $this->upload->display_errors();
}
else
{
$this->upload->do_upload('userfile');
}
Views:
<form method="post" enctype="multipart/form-data">
<input type="file" id="userfile" name="userfile"/>
<input type="submit" name="submit" id="sumit" value="Test" />
</form>
There is bug in that library maybe this will help
I have two upload scripts that do very similar functions just with different directories.
function town_upload() {
//Do not create a thumbnail
$create_thumb = false;
//Create the path for the upload
$path = './public/uploads/towns/' . $this->uri->segment(3);
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = $this->image_upload_overwrite;
if ($this->input->post('image_type') == 'main_image') {
$config['file_name'] = 'main.jpg';
$create_thumb = true;
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('upload_error', array('error' => $this->upload->display_errors()));
} else {
//sucess so now we create the thumbnail
$this->session->set_flashdata('upload_success', $this->upload->data());
Example: If I had a town with the ID of 6, the upload directory would be public/uploads/towns/6. This works perfectly fine as the directory is created when the town is created. I have nearly the exact same function for hotels except the path is public/uploads/hotels/[ID]. I have set the permissions to 777 just to remove that from the equation for now.
I cannot figure this out as they are both very similar functions. Here is the hotel upload code:
function hotel_upload() {
//Create the path for the upload
$path = './public/uploads/hotels/' . $this->uri->segment(3);
chmod($path, 0777);
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg';
$config['max_size'] = '2048'; //kilobytes (2048 = 2mb)
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
if ($this->input->post('image_type') == 'main_image') {
$config['file_name'] = 'main.jpg';
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$this->session->set_flashdata('upload_error', array('error' => $this->upload->display_errors() . ' <br/>Path: ' . $path . '<br/>Image Type: ' . $this->input->post('image_type')));
} else {
//sucess so now we create the thumbnail
$this->session->set_flashdata('upload_success', $this->upload->data());
The hotel function gives me the error "The upload destination folder does not appear to be writable.".
Here are the pages HTML forms:
TOWN:
<?php echo form_open_multipart('admin/town_upload/' . $this->uri->segment(3));?>
<input type="file" name="userfile" size="20" />
<br /><br />
<p>
<input type="radio" name="image_type" checked="checked" class="radio" value="main_image" /> <label>Main Image</label>
<input type="radio" name="image_type" class="radio" value="other_image" /> <label>Other Image</label>
</p>
<input type="submit" class="submit short" value="upload" />
<?php echo form_close(); ?>
HOTEL:
<?php echo form_open_multipart('admin/hotel_upload/' . $this->uri->segment(3));?>
<input type="file" name="userfile" size="20" />
<br /><br />
<p>
<input type="radio" name="image_type" checked="checked" class="radio" value="main_image" /> <label>Main Image (shown when visitors search the website)</label>
<input type="radio" name="image_type" class="radio" value="other_image" /> <label>Standard Image</label>
</p>
<input type="submit" class="submit short" value="upload" />
<?php echo form_close(); ?>
Thanks in advance! Been working on this for hours now.. :(
Your code doesnt check the chmod works. For example, by default, apache loves to run as its own user or nobody, not you as a user. So, the chmod in your file may find the user its currently running as doesnt own the file, so the chmod fails.
First thing I would check would be the user PHP thinks it runs as, and the owner of the file.
There is the is_writable function, you could check the directory is writable.
I just ran into the same problem and found a possible reason for this behaviour. In my case, I had created the upload directory a while back. I upgraded php which put in a new php.ini file that had safe_mod = On. And now when I recreated the directory after erasing it, the new php parameters kicked in throwing this error message.
Could it just be possible that you created the town dir tree before a PHP upgrade and your hotel dir tree afterwards?
Okay, just came back to this. I think permissions got messed up somewhere, I destroyed and recreated the folder and it seemed to work.