I am creating some file upload functionality utilizing the upload library within the Code Igniter framework and I am having an issue where I keep on getting an error back stating that my file upload path is incorrect. I have checked many times and there is no issue with the file structure and the files definitely exist.
Here is my model upload code (I am dieing to check where I am within the process):
class Gallery_model extends CI_Model {
var $gallery_path;
function __construct() {
parent::__construct();
$this->gallery_path = realpath(APPPATH . '../images/profileImages');
//$this->gallery_path = realpath($this->config->base_url() . 'images/profileImages');
}
public function doUpload() {
$config = array(
'allowed_types' => 'jpg|jpeg|png|gif',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
if($this->upload->do_upload()) {
$data = array('upload_data' => $this->upload->data());
die(print_R($data));
} else {
$error = array('error' => $this->upload->display_errors());
die(print_R($error));
}
}
}
My images folder is outside of the application and is called 'images', which contains a folder called 'profileImage' where I want this files to save. I get the following error:
'The upload path does not appear to be valid.'
I can't for the life of me figure out why this path is not working when it definitely exists. Does anyone have any ideas?
Thanks
Codeigniter will only return that error message if $config['upload_path'] is blank or the php command is_dir($config['upload_path']) returns false. Check to make sure the 'uploads' folder is in the same directory as main index.php file and that the directory name is the same case
Is your images folder in the web root? If is this should work
$this->gallery_path = './images/profileImages/';
Related
I have a web page form that needs to select image and upload to a file, but for some reasons i dont know the image is not being detected. I might be missing something. The image folder is was created in the same folder as the application and system. Find the code below
View
<div id="gallery">
<div id="upload">
<?php echo $error;?>
<?php
echo form_open_multipart('index.php/favoriteMemories/pleasurable');
echo form_upload('userfile');
echo form_submit('upload', 'Upload');
echo form_close();
?>
</div>
</div>
Controller
public function pleasurable() {
$config = array(
'upload_path' => '../images/',
'allowed_types' => 'gif|jpg|png|jpeg',
'max_size' => '1000000',
'overwrite' => TRUE,
'remove_spaces' => TRUE,
'encrypt_name' => FALSE
);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('pleasurable', $error);
} else {
$this->upload->do_upload('userfile');
$this->load->view('pleasurable');
}
}
Make sure the following,
In your controller function you don't need to use the upload code twice.
$this->upload->do_upload('userfile');
its already in the if condition you don't need to call it in else part.
Make sure you have loaded the upload library because I didn't see that in controler function, you might load it in constructor or autoload but make sure.
$this->load->library('upload', $config);
Make sure you image folder path is correct and writable
Check the image type and size you provided in the $config array.
if you see any error message please post it for more help.all the best.
i have gone through your code. there is no error in ur code. accept the upload path.
if you change it to 'upload_path' => 'image/',
it will save the image in image folder located in root. just make sure the image folder is writable. you can print the upload data and change the folder accordingly.
use print_r($this->upload->data());
to check the uploaded data
I'm creating a library as part of a project and one of the methods is a wrapper for the upload helper.
The method:
public function upload(){
echo "Doing upload";
$config['upload_path']= RESOURCE_PATH . "Downloads";
$config['allowed_types']='pdf|doc';
$config['max_size']='10000';
//echo $config['upload_path'];
$this->CI->load->library('upload',$config);
if(!$this->CI->upload->do_upload()){
echo "Couldn't do the upload";
echo $this->CI->upload->display_errors();
echo $config['upload_path'];
}
else{
echo "Could do the upload";
}
}
I've checked the directory permissions of the Downloads folder and that it exists but I'm getting the following error: "The upload path does not appear to be valid."
How do I resolve this issue?
EDIT:: I created a symlink so my directory strucutre really looks like:
www -> /home/user/Dropbox/www/appname
Note about variables used:
$this->CI = &get_instance(); // Defined in custom library class
define('RESOURCE_PATH', APPPATH . 'views/resources/'); // Defined in constants.php
Figured out the idiotic problem.
I was autoloading the library and some how when I was trying to
initialize the configuration by $this->load->library('upload',
$config); it wouldn't do so.
Instead I put my config files in config/upload.php
The other method to do so would have been
$this->upload->initialize($config);
See this answer.
Add this line
$this->CI->upload->initialize($config);
after
$this->CI->load->library('upload',$config);
I'm trying to upload a css file user codigniter's upload class. Here is the controller:
public function uploadcssfile()
{
$config['upload_path'] = './stylesheets/stores/'.$memberid.'/';
$config['file_name'] = 'main.css';
$config['allowed_types'] = 'css';
$config['overwrite'] = true;
$this->load->library('upload', $config);
if (! $this->upload->do_upload('filefieldname'))
{
die($this->upload->display_errors());
}
}
I just want to upload a file of type css, yet codeigniter always gives this error:
The filetype you are attempting to upload is not allowed.
I know, this is really weird but it worked for me:
in application/config/mimes.php, line 77
'css' => 'text/css',
change to:
'css' => array('text/css','text/x-c'),
To see, if your development environment has its own sense of humour and can add new mime types to any text files (my case), check your mimes like this:
if ( ! $this->upload->do_upload(-your-form-input-name-) ){
print_r($_FILES['-your-form-input-name-']['type']);
exit($this->upload->display_errors(). 'File Type: ' . $this->upload->file_type);
}
If an upload fails it will show you what mime type your server is actually getting to deal with.
PS. Now that there is an answer to your question, go here and help me :)
function upload($path){
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = 2000;
if($path =='profile'){
$config['upload_path'] = '../assets/uploads/avatars';
$config['file_name'] = $this->id;
}
if($path =='company'){
$config['upload_path'] = '../assets/uploads/company';
$config['file_name'] = $this->id.'_company';
}
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
if($path == 'profile'){
$this->db->update('be_user_profiles',array('avatar' => $image_data['file_name']), array('user_id' => $this->id));
}
if($path == 'company'){
$this->db->update('be_user_profiles',array('company_logo' => $image_data['file_name']), array('user_id' => $this->id));
}
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->upload_path,
'maintain_ratio' => true,
'width' => 500,
'height' => 500
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
This is my upload function, and it works fine when $path = 'profile', but when it is company, it won't upload to the "company" folder...
Is there any reason it should be so?! I'm at a loss here...This function works when it goes to the avatar folder, but not if it goes to the company folder...
I had a similar problem with multiple folders. The way I fixed was to use to use the initialize function instead of passing the config as an argument to the load library function.
$this->upload->initialize($config);
You could load the library then set your config and call the initialize method.
Hope this helps.
I think all suggestions are really good, you can also make sure you are posting the right data in your form:
$this->upload->do_upload(); is by default expecting the form name to be 'userfile'
also I find sometimes really usefull to have some errors displayed...so instead of just $this->upload->do_upload(); try something like
if (!$this->upload->do_upload($userfile)) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_error', $error);
}
and then add a file in your views called upload_error.php with the following code in it:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
</body>
</html>
good luck!
Try to unset config before setting another config like this.
The following code unset config and clear image library.
You need to clear your lib.
//Unset config for the next one
unset($config);
$this->image_lib->clear();
From CI manual...
$this->image_lib->clear()
The clear function resets all of the values used when processing an image. You will want to call this if you are processing images in a loop.
Hmmm, Here are two suggestions:
Make sure your company folder is writable
What $this->id returns? if it's returning the file name WITH the extension then this is what causing your upload process to fail because then you'll have a file name my_image.png_company which is not an allowed type.
Please refer to the documentation page for more info.
$config = array(
'upload_path' './assets/upload/profilepic/',
'allowed_types' => 'JPG|JPEG|PNG',
'overwrite' => TRUE,
'file_name' => date('YmdHis').''.rand(0,9999)
);
its not works for JPEG file type
also i had written in small latter but it didn't works.
please Share if you have something
JPEG files are not upload Others file are inserted easely.
I've got an image uploader that doesn't upload images. I'm not receiving any errors and i've checked the folder permissions and even on 777 no images are getting uploaded.
You can see the code here: http://pastebin.com/gvH1dKh9
The value for gallery_path is /home/domain/public_html/assets/images/gallery
The value for gallery_path_url is http://domain.com/assets/images/gallery/
I have used this same code on another site with zero problems. I'm not sure what the problem is with this site?
Use
$this->load->library('upload', $config);
instead of
$this->upload->initialize($config);
In my case it was helpful
Models are intended for interaction with databases. Try moving your upload code into the controller, then if needed take the returned data ($this->upload->data();) and pass that to a model for insertion to a database.
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => '/uploads',
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$this->Gallery_model->insertImageData($image_data);
}
}
Try doing some error checking on your upload:
if(! $this->upload->do_upload('Filedata')){
echo $this->upload->display_errors();
}
$upload_info = $this->upload->data();
echo var_dump($upload_info);