I have written code for uploading a file in CodeIgniter.
Here is the view file code
<form method='post' type="get" enctype="multipart/form-data" action="" class="apply_form" id="apply_form">
<input type="file" name="submit_resume" id="submit_resume" class="submit_resume" value="browse"/>
Here is the controller file code
$this->load->helper('url');
$this->load->helper('html');
$this->load->helper('form');
$this->load->library('upload');
$config['upload_path'] = $this->base_url.'/uploads/';
$config['allowed_types'] = 'docx|doc|pdf';
$config['max_size'] = '1000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$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);
}
But nothing is uploading in the uploads folder.
Cant figure out what the issue really is!
Some changes I would do:
$config['upload_path'] = './uploads/';
Mention only the relative path and not absolute path.
if ( ! $this->upload->do_upload('submit_resume')) //here see the file name
{
$error = array('error' => $this->upload->display_errors());
print_r( $error );die;
}
Just to view the error you are getting.
You must have the correct permission set in your uploads folder too.
The permission of the folder in which the file is getting uploaded might be the issue.
$config['upload_path'] = './uploads/';
For changing the permission of above folder, use following command in your terminal -
chmod -R 750 /uploads/
Explanation:
chmod 750 changes to permission to read and write for the user.
-R makes is recursive.
Related
i am trying to upload an image in codigniter but unable to do so when I try it shows me an error array to string conversion
<?php
public function do_upload() {
$config['upload_path'] = base_url().'assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload();
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('success_msg', $error);
redirect('admin/theme');
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
?>
base_url() is return HTTP path not directory path, you should use directory path for upload content.
change
$config['upload_path'] = base_url().'assets/uploads/';
to
$config['upload_path'] = getcwd().'assets/uploads/';
you can use getcwd() native PHP function to get project root directory, and CI is also provides constant variable FCPATH for root directory.
Array to string conversion usually shows up, when you try to echo an Array.
so it seems, that the problem lies on this line:
echo validation_errors();
To avoid this error but still print out the values in the array use Foreach().
This Code should solve your problem:
foreach (validation_errors() as $error) {
//I assume you get an array as a return value
echo $error;
}
EDIT
This answer doesn't help anymore, as the author edited his question
You are uploding your files two time. After first uploding every thing is vanished and you have nothing to upload
$this->upload->do_upload();// first time
if (!$this->upload->do_upload()) {// second time
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('success_msg', $error);
redirect('admin/theme');
}
Just comment it
// $this->upload->do_upload();// comment this line
EDIT
You can send your message into set_flashdata() as
if (!$this->upload->do_upload('file_name')) {// add file name here
$error = $this->upload->display_errors();// remove array from here
$this->session->set_flashdata('success_msg', $error);
redirect('admin/theme');
}
To read a flashdata variable:
$this->session->flashdata('success_msg');
I am trying to upload video in server using Codeigniter, In the localhost i can do it, but when in the server it does not work. Even though do_upload() function returns true.
When i'm testing the server root, print_r($_SERVER['DOCUMENT_ROOT']); it returns
/var/www/mjpp/data/www/supps.mydomainname.com
Also the
$updata = $this->upload->data(); returns
[file_path] => /var/www/mjpp/data/www/supps.mydomainname.com/public/video
Can you tell me why /var/www/mjpp/data/ is appearing before my main domain name , and is it the cause of any error for uploading video?
Thank you in advance, sorry for my bad english.
/var/www/mjpp/data/www/supps.mydomainname.com is absolute location of your document root in a system in general. However you do not usually have access to anything above your user dir.
public/video or /var/www/mjpp/data/www/supps.mydomainname.com/public/video (as absolute path) is what you pass to Upload library as upload_path. This is where the library will try yo upload the files. This folder should exist and be writeable (have 0777 permissions on it for example).
No need to use Document Root, Codeigniter itself having configurations when file uploading.
Here is sample code for image uploading,
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())
{
$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);
}
}
You just need to change the configuration($config array), for your issue you should use the $config['upload_path'].
Follow codigniter file upload tutorial
Dont use $_SERVER['DOCUMENT_ROOT'] in CI, use URL Helper
$this->load->helper('url');
echo base_url();
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'm trying to upload profile pics during a registration process with CI ($me !== 'experienced' )
Here is my method ... nothing strange
function do_upload()
{
$config['upload_path'] = base_url('/upload_pic/');
$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() )
{
$data['message'] = $this->upload->display_errors() . $config['upload_path'];
$this->load->view('profile_photo_view', $data);
}
else
{
$data['message'] = $this->upload->data();
$this->load->view('upload_success', $data);
}
}
.. but it is failing. As you can see, I have the upload path passing to my fail page for display, and this is what it is showing..
The upload path does not appear to be valid.
http://localhost:8888/MY_SITE/upload_pic
That is where my upload folder is. And I have full 777 permissions on it..
.. any idea what could be wrong? And will trailing slashes always be stripped like that?
base_url will give you...wait for it...a URL :)
You need a path for uploads.
$config['upload_path'] = '/full/path/to/upload_pic/';
or you can use relative paths:
$config['upload_path'] = '../upload_pic';
but you have to give it a path, not a URL.
As #stormdrain pointed out, base_url() is there to get the URL, not a filesystem path. Try:
$config['upload_path'] = dirname(FCPATH) . '/upload_pic/';
Try this
$config['upload_path'] = base_url() . '/upload_pic/';
or inside index.php append this
define('UPLOADS', 'upload_pic');
then set config to
$config['upload_path'] = UPLOADS;
I am getting this error:
The upload destination folder does not appear to be writable.
But is_dir and is_writable both return true.
$config['upload_path'] = './work_order_doc/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']= '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$work_order_file="work_order_file";
var_dump(is_dir($config['upload_path']));
var_dump(is_writable($config['upload_path']));
if ( ! $this->upload->do_upload($work_order_file))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
$temp="";
}
else
{
$upload_data = $this->upload->data();
echo $temp=$upload_data['file_name'];
}
Maybe your host have blocked file uploads?
You can always try to specify full path to the upload directory.
Your PHP configuration may be not allowing you to get to the file/directory, try checking open_basedir in php.ini and make sure it is in allowed paths.