Cannot Upload Excel file in CodeIgniter - php

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

Related

codeigniter upload path does not appear to be valid

Problem is When upload a image its shows same error every time "The upload path does not appear to be valid". Tried so much solution but still same problem with it..Please help.
Controller:
public function updateprofile()
{
/***get current user data***/
$this->load->model('Admin_main','userinfo');
$data=$this->userinfo->getuserdata();
$this->load->model('Admin_main','updateprofile');
$result=$this->updateprofile->updatemyprofile();
if($data->identity=='admin')
{
redirect('admin/myprofile');
}
else
{
redirect('campuser/myprofile');
}
}
Model:
if($_FILES['fileToUpload']['name']!='')
{
$config["upload_path"] = './uploads/';
$config["allowed_types"] = 'jpg|jpeg|png|gif';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('fileToUpload')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
$mypic = array('profile_pic'=>$picture);
$this->db->set($mypic);
$this->db->where('id', $userid);
$uppic=$this->db->update('mokhayam_users');
}
else
{
echo $this->upload->display_errors();
}
}
View:
<form class="form-horizontal" action="admin/updateprofile" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-sm-2 control-label">Profile Picture</label>
<div class="col-sm-10">
<label for="fileToUpload"><img src="assets/admin/dist/img/gul.jpg" id="blah1" class="profile-user-img img-responsive img-circle"/></label><input type="file" name="fileToUpload" id="fileToUpload" onchange="$('#blah1')[0].src = window.URL.createObjectURL(this.files[0])">
</div>
</div>
<input type="hidden" name="userid" value="2">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
Thanks in advance..
If your codeigniter version is 3.x please change library file (system/library/upload.php) version codeigniter 3.x to 2.x
and then try
$config["upload_path"] = APPPATH.'/uploads/';
......
Without changing anything in your library, the solution is to initialize the config just after loading the upload library in your controller, follow this code
// Upload Image
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '7048';
$config['max_width'] = '7000';
$config['max_height'] = '7000';
// Create folder (Uploads) if not exists
if (!is_dir($config['upload_path'])) {
mkdir($config['upload_path']);
}
// Load library Upload
$this->load->library('upload', $config);
// Initialize the Upload library with current $config
$this->upload->initialize($config);
$this->upload->do_upload();
if ($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
$page_image = $_FILES['userfile']['name'];
} else {
$errors = array('error' => $this->upload->display_errors());
$page_image = 'noimage.jpg';
}
// Set message
$this->session->set_flashdata('page_created', 'Your File(s) has been uploaded');

CodeIgniter Upload Image no input

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

Couldn't upload file in codeigniter text form

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
}
}

Uploading multiple files at a time in codeigniter with different names

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

The upload destination folder does not appear to be writable. (Codeigniter)

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.

Categories