I am trying to upload a file in CodeIgniter when on live server. It is working perfectly on localhost but when I try to upload it on live server the upload library is not working, can you please tell me what I am doing wrong? Do I need to change something in php.ini of live server?
Here is my code:
public function do_upload()
{
$config = [
'upload_path' => './uploads',
'allowed_types' => 'jpg|gif|png|jpeg',
'max_size' => 1100000000,
'max_width' => 102400000,
'max_height' => 76800000,
];
// print_r($config);
// exit();
$upload= $this->load->library('upload');
$this->load->library('upload', $config);
$this->upload->initialize($config);
// var_dump($config);
var_dump($upload);
// exit();
if(!$this->upload->do_upload('imagefile'))
{
$upload_error = $this->upload->display_errors();
$this->load->view('site/timeline/timeline_profile',compact('upload_error'));
}
else
{
$post = $this->input->post();
unset($post['submit']);
$upload_data = $this->upload->data();
$file_name=$_FILES['imagefile'];
$this->load->model('Timeline_model');
$this->Timeline_model->upload_model($post,$file_name);
echo $image_path= base_url("uploads/".$upload_data['raw_name'].$upload_data['file_ext']);
}
}
on var dump the value it is showing me null.
Please follow code in File Uploading Class exactly and it will work and make sure you have write permissions to upload folder in your server.
Related
I get following error when trying to upload .ini files
"The filetype you are attempting to upload is not allowed."
Below is the function for uploading files
private function _upload_config_file() {
$config['upload_path'] = APPPATH . 'config/ini/';
$config['allowed_types'] = 'ini';
$config['max_size'] = '2000000';
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$this->upload_status = 'File uploaded succesfully !';
return true;
} else {
$this->upload_status = $this->upload->display_errors();
return false;
}
}
In View
$upload_file_attr = array(
'name' => 'userfile',
'id' => 'userfile',
'disabled' => 'disabled'
);
echo form_open_multipart('sys_setting', $form_sys_setting_multi);
echo form_label('Configure Manually' . form_checkbox($upload_checkbox, '', FALSE, $js_checkbox));
echo form_button($download_button);
echo form_upload($upload_file_attr);
echo form_reset($upload_reset);
echo form_submit($upload_submit);
echo form_close();
Is there any changes to be made in php.ini for uploading .ini files ?
Or Is it codeigniter's upload library that doesn't allow to upload .ini files ?
Need suggestions to solve this issue.
you forgot to put the field_name
Try adding this:
if ($this->upload->do_upload('userfile')) { //parameter should the your input file name attribute
$this->upload_status = 'File uploaded succesfully !';
return true;
} else {
$this->upload_status = $this->upload->display_errors();
return false;
}
I got this issue solved.In config/mimes.php modified $mimes array
$mimes = array('ini' => 'application/octet-stream')
If any one knows how to solve this issue without modifying the core files, *Please do comment.*
this is a code for swf fiel uploading but the problem is it works fine on localhost but not in the remote server.here is my controller
function do_upload()
{
$pid=$this->input->post('Page_id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'swf';
$config['max_size'] = '1048';
$config['file_name'] =$pid;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else{
$data=array(
'id'=>$pid,
'link'=>base_url()."uploads/",
'file_name'=>$config['file_name']
);
$this->file_upload_model->upload_data($data);
$this->upload_success();
}
}
i have not done anything except just uploading the path and file name to the database in model. see the demo here
Try this
$pid=$this->input->post('Page_id');
$config['file_name'] = $pid;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'swf';
$config['max_size'] = '1048';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload())
{
//upload successful
//do something
}
else
{
$this->session->set_flashdata('error',$this->upload->display_errors());
redirect('controller/method');
/*
In the view file you can echo the error like this
echo $this->session->flashdata('error');
*/
}
You have pass the the field name in the do_upload call like this:
$config = array(
'allowed_types' => 'jpg|jpeg|swf |png', // pipe seperated file types
'upload_path' => './uploads/', // should be the root path
'max_size' => '1048',
'file_name' => $pid
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('your_field_name')) {
// like $this->upload->do_upload($pid)
$error = array('error' => $this->upload->display_errors());
} else {
$swf_data = $this->upload->data();
}
Hope it makes sense
it may be due to PHP server version and it's option.
1).go to cpanel account
2).click "select php version", in the software section.
3).tap the "fileinfo" chexbox in the php option and save.
now you can upload file perfectly.
Now i've tried most of the fixes that i've read, most of them mention about APPPATH, base_url(), real path and etc. but i really don't know why all of them didn't work, what worked for me is that i've used the actual path, not a url but the one with the C:\xampp\htdocs.. blah blah blah.. now i've read one thread that url and directory aren't the same thing.. and the upload_path accepts only directory path and i mean the actual location of the uploads folder on the server not the URL.. now my question is how come APPPATH don't work. as what i know it the actual directory path. but when i tried to display is it return only "../applicaiton/" what really is the best path to be used on the $config['upload_path'] on the upload.php specially when deploying it to an actual server it is really a nuisance finding the directory path of your upload folder, NOTE im not using the initialize() method i'm putting my configs on config/upload.php
EDITS:
i have this on a separate file... upload.php
<?php
$config['upload_path'] ='./uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
and this is my controller
if($this->upload->do_upload('image'))
{
//Did something here
}
else
{
//Did something for errors like display_errors()
}
and end result is it displays "the upload path doesn't seem to be valid"
and i've tried these line of code also
Trial 1:
$config['upload_path'] ='./uploads/';
Trial 2:
$config['upload_path'] ='uploads/';
Trial 3:
$config['upload_path'] ='/admin/assets/uploads/';
Trial 4:
$config['upload_path'] ='./admin/assets/uploads/';
Trial 5:
$config['upload_path'] ='admin/assets/uploads/';
the only thing that works is this
$config['upload_path'] ='C:\xampp\htdocs\practice\admin.abcgencon\admin\assets\uploads'';
and using the last part as a path is kinda messy so i've tried also APPPATHbut it doesn't work also it also display "../application"..
as #cryptic said i've posted this code snippet.
Question, you tried realpath and APPPATH seperately?
In Codeigniter APPPATH points to the application folder.
Example: (place your folder outside the application folder, just saying if you did not do that way) let's say the folder where we want to place the files called images.
So what you need to do is to combine realpath() and APPPATH
$image_path = realpath(APPPATH . '../images');
and pass it to your config
$config['upload_path'] = $image_path;
Create your file upload directory say uploads outside of application directory or at the root directory of CI, and set the upload path as follows:-
$config['upload_path'] = realpath(FCPATH.'uploads');
FCPATH: Path to the front controller where index.php exists (root of CI)
The above code runs on both server and local.
This is how file uploading is done in CI
$cat_image_name = $_FILES["cat_image"]["name"] ;
//file uploading params
$config['upload_path'] = './uploaded_files/categories';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $image_id."_ipad";
$config['remove_spaces'] = TRUE;
//Loading Library - File Uploading
$this->load->library('upload', $config);
//Upload the image(Ipad)
if (!empty($cat_image_name))
{
$this->upload->do_upload('cat_image');
$data = array('upload_data' => $this->upload->data());
$category_image_ipad = $data['upload_data']['file_name'];
$img_extension = $data['upload_data']['file_ext'];
}
try this
$config['upload_path'] = '../uploads/;
It's working very well for me
use it:
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/uploads/";
//$this->config->item('upload_path');
file upload requires a multipart form. For this, you must have included a form helper.
goto config folder, click autoload and find the $autoload['helper'] = array(); and put 'form' :
$autoload['helper'] = array('form');
for controller:
$config = array(
'upload_path' => "./assets/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->user_model->create_photo($post_image);
redirect('');
}
for model:
public function create_photo($post_image){
$data = array(
'pri' => $this->input->post('price'),
'descrip' => $this->input->post('title'),
'post_image' => $post_image
);
return $this->db->insert('table_name', $data);
}
Tested on Windows & Linux:
$path = realpath(".");
$path .= "/uploads/profile_images";
Here uploads is the uploads directory on root.
FCPATH: front controller path where index.php exists or root folder
APPPATH: application folder
Create a uploads folder under root folder:
$config['upload_path'] = realpath(FCPATH.'uploads');
Create a uploads folder under application folder:
$config['upload_path'] = realpath(APPPATH.'uploads');
In case: If you have created the uploads folder outside of root folder:
$config['upload_path'] = realpath($_SERVER['DOCUMENT_ROOT'].'/uploads');
It is very simple:
Just copy your all $config in new php file called upload.php and put it in cofig folder.
You path will work ok.
Here is upload.php file contents:
<?php
$config['upload_path'] = site_url().'uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg|html|pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_ext_tolower'] = TRUE;
$config['overwrite'] = FALSE;
$config['max_filename'] = 0;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['detect_mime'] = TRUE;
$config['mod_mime_fix'] = TRUE;
?>
But please keep $config data in upload_file controller as well.For example:
public function do_upload(){
$config['upload_path'] = 'uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg|html|pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_ext_tolower'] = TRUE;
$config['overwrite'] = FALSE;
$config['max_filename'] = 0;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['detect_mime'] = TRUE;
$config['mod_mime_fix'] = TRUE;
//Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
It is simple...
I got a weird problem when uploading images via "upload" library.
When i have huge pics, like 1,8mb big it always uploads them upside down.
Otherwise it gets uploaded normally.
my model for upload looks like this
function do_upload() {
$this->gallery_path = realpath(APPPATH . '../public/images');
$config = array(
'upload_path' => $this->gallery_path,
'allowed_types' => 'jpg|jpeg|gif|png',
'max_size' => 2000
);
$this->load->library('upload',$config);
$this->upload->do_upload();
$image_data = $this->upload->data();
//to db
$filename=$image_data['file_name'];
$this->db->set('name', $filename);
$this->db->insert('images');
}
could anyone please just explain why it gets like this? I've tried to increase max size to see if it would help but it didn't.
Would be more than thankful for some help :)
Try setting your config for upload:
$config['max_size'] = 0;
This is my upload model
function upload_avatar()
{
$id = $this->tank_auth->get_user_id();
//config upload parameters and upload image
$config = array(
'allowed_types' => 'jpeg|jpg|png',
'upload_path' => $this->upload_path,
'max_size' => 2048,
'encrypt_name' => TRUE,
'overwrite' => FALSE,
);
$this->load->library('upload', $config);
$this->upload->do_upload();
//get upload data, config, resize uploaded image, save in avatars subfolder
$image_data = $this->upload->data();
if ($image_data['file_size'] < 2048) {
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->upload_path . '/avatars',
'maintain_ratio' => TRUE,
'width' => 125,
'height' => 125
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//only burn avatar path to user_profiles table if no upload errors
if (!$this->upload->display_errors()) {
$data = array('avatar' => base_url() . 'images/avatars/' . $image_data['file_name']);
$this->db->where('id', $id);
$this->db->update('user_profiles', $data);
}
//delete the original file from server
$this->load->helper('file');
unlink($image_data['full_path']);
} else {
echo $this->upload->display_errors();
}
}
I can't get the error message to echo straight to the browser when I try uploading a file > 2MB.
To be fair, CI ignores this large file, and uploads correctly when a file is < 2MB.
The only thing is that I can't get the error message to show on the front-end to give the suer some feedback.
Any ideas what's wrong here?
$config['upload_path'] = 'uploads/category/'.$id.'/';
//echo $file_name;die;
//echo $config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload');
foreach ($_FILES as $key => $value) {
//print_r($key);
if (!empty($key['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
// echo 'test';die;
// rmdir('uploads/category/'.$id);
$errors = $this->upload->display_errors();
flashMsg($errors);
}
}
}
try this!!
Is your post_max_size limit less than 2MB? (http://ca3.php.net/manual/en/ini.core.php#ini.post-max-size) If so the file may have been discarded before your code is invoked.
Update:
If you take out your function call in the else block, and just drop in an exit('too big'); are you able to see errors then? If so there may be an issue with how you're pasing the call off.