Show Error: The upload path does not appear to be valid - php

I am try to upload a image by php codeigniter but show "The upload path does not appear to be valid."
This Action is performed in xampp server and window 10
public function do_upload($username) {
$config = array(
'upload_path' => "./assets/images/",
'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()) {
$data = array('upload_data' => $this->upload->data());
print_r($data);
} else {
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
This code i used i chek many time but i not found error
What error in the code,
If i use this same code in other project it will work but not in the main project , Please help, Thanks in advance

In CodeIgniter you should always try and use the constant APPPATH to find the application folder.
APPPATH is the best because it dynamically retrieves the proper filesystem directory structure no matter which operating system you are on. This ensures that all file paths in your code are going to be correct.
However if you are not trying to get to the Application folder. You can use the FCPATH constant which dynamically points to the front controller of your application.
You are getting this error because the path to your /assets/images folder is not valid. You need to MAKE SURE that the path is correct.

first you should check you have created folder assets/images folder in root directory because in config this path is defined
$config = [
'upload_path' => "./assets/images/",
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
];
$this->load->library('upload',$config);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div style="color:red;" class="error">', '</div>');
if$this->upload->do_upload()){
$img = $this->upload->data();
$img_name = base_url('uploads/'.$img['file_name']);
$data = $this->input->post();
$data['image_path'] = $img_name;
unset($data['submit']);
return $this->flash_message(
$this->articlemodel->add_article($data),
'Article added sucessfully',
'Article added to failed'
);
}
else
{
$upload_error = $this->upload->display_errors;
}

It is very simple to resolve
If directory inside the application folder
application -> assets -> images
'upload_path' => APPPATH. 'assets/images/';
If directory outside of the application folder
root folder -> assets -> images
'upload_path' => FCPATH. 'assets/images/';

Related

Uploading files in codeigniter

A Problem was encountered while attempting to move the uploaded file to the final destination
Unable to upload the file in codeigniter using their upload class, upload directory is also exist,
below is a code
$config = array(
'upload_path' => './upload_dir/users_docs/',
'allowed_types' => 'gif|png|jpeg',
'max_size' => 0,
'max_width' => 0,
'max_height' => 0,
'file_name' => $this->currTime,
);
$this->load->library('upload', $config);
if (!empty($_FILES['company_logo']['name']) && !$this->upload->do_upload('company_logo')) {
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error_message', $error['error']);
redirect('home');
}
$data = array('upload_data' => $this->upload->data());
There was an issue in the 'file_name' => $this->currTime, the format of time was not correct, Apostrophe where coming in the filename var, I just changed it to something else, now it's working, Thank you

Codeigniter AJAX file upload not working

I am trying to upload a file to the same directory in which application folder is present
-application
-images
-system
-others...
The code that I am trying is
$config = array(
'upload_path' => "images",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024",
);
if(isset($_FILES["image"]["name"])) {
if (0 < $_FILES['image']['error']) {
echo 'Error Occurred During Upload Of File ' . $_FILES['image']['error'];
} else {
$this->load->library('upload', $config);
if(!$this->upload->do_upload('image')) {
echo $this->upload->display_errors(); //ALWAYS THIS LINE IS EXECUTING
}
else {
echo "FILE UPLOADED";
}
}
} else {
echo "Please Select A File";
}
The problem is that always the line having comment inside innermost if is executing and it says <p>The upload path does not appear to be valid.</p>.
When I tried to check whether that directory exists using is_dir, it also says that directory exists and also I tried giving it 777 file permission but still I am getting the same errors. I also tried most of the similar answers online but none worked.
Please HELP
Try this:
$config = array(
'upload_path' => "./images/",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024",
);

upload multiple documents with PHP Codeigniter

I am working on a user registration form in which the user is required to upload his profile image and a document for verification purposes. These are the two different fields in the form. The code works fine but the problem is that it only uploads the profile image and stores it in the verification documents folder. It does not upload the verification document. It works fine if I remove or comment one of the field.
Below is my controller function.
/** UPLOAD PROFILE IMAGE **/
$prof_pic = $_FILES['profile_pic']['name'];
$config = array ('upload_path' => './images/tutors/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE
);
$this->load->library('upload', $config);
$this->upload->do_upload('profile_pic');
/** UPLOAD VERIFICATION DOCUMENT **/
$tut_ver_docs = $_FILES['tut_ver_docs']['name'];
$new_config = array ('upload_path' => './emp_verification_documents/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE
);
$this->load->library('upload', $new_config);
$this->upload->do_upload('tut_ver_docs');
Try replacing:
$this->load->library('upload', $config);
with :
$this->load->library('upload');
$this->upload->initialize($config);
replace
$this->load->library('upload', $new_config);
with
$this->upload->initialize($new_config);
I'm not entirely sure why you're loading the upload class twice:
$this->load->library('upload');
$this->upload->upload_path = './images/tutors/';
$this->upload->allowed_types = array('jpeg','jpg','png');
foreach($_FILES as $name => $file){
if($name == 'tut_ver_docs'){ //if its the verification document, change the upload path
$this->upload->upload_path = './emp_verification_documents/';
}
if( ! $this->upload->do_upload($name){
//catch error
}
}
I did not tested it But I think this should work for you
/** UPLOAD PROFILE IMAGE **/
$this->load->library('upload');//load the library
$config = array ('upload_path' => './images/tutors/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE
);//config for profile picture
$this->upload->initialize($config);
if (!$this->upload->do_upload('profile_pic'))
{
print_r($this->upload->display_errors());//upload fails.you can print the error or display somewhere else
}
/** UPLOAD VERIFICATION DOCUMENT **/
$tut_ver_docs = $_FILES['tut_ver_docs']['name'];
$new_config = array ('upload_path' => './emp_verification_documents/',
'allowed_types' => "jpeg|jpg|png",
'overwrite' => TRUE
);
$this->upload->initialize($new_config);
if (!$this->upload->do_upload('tut_ver_docs'))
{
print_r($this->upload->display_errors());//upload fails
}

I am getting "The upload path does not appear to be valid." even when I am using absolute path

I am trying to upload a file using codeigniters file upload library. But I am getting this error "The upload path does not appear to be valid.". But the path I am using is absolute path. The permission of the folder is 0666. Here is the code, given below -
$this->load->library('upload');
$this->upload->initialize(array(
"upload_path" => '/var/www/html/educorp/images/students/',
"overwrite" => TRUE,
"encrypt_name" => TRUE,
"remove_spaces" => TRUE,
"allowed_types" => "jpg|jpeg|png",
"max_size" => 300,
"xss_clean" => FALSE
));
if($this->upload->do_multi_upload('pictures')){
echo 'done';
} else{
$this->session->set_flashdata('errors', true);
$this->session->set_flashdata('messages', $this->upload->display_errors());
redirect('student/show_upload_pictures_form');
}
And why don you use instead:
"upload_path" => APPPATH . '../images/students/',
I don't understand why but when I changed the permission to 0777 it started to work.

Codeigniter Uploading files into gallery folder

This is what im trying to do in codeigniter:
I have member profile pages that all have their own gallery of photos. The user to be able to login and upload photos. I want the photos to then be placed inside of a folder with the usersname. So if the directory doesnt exsit for the user i want it to be created once he uploads a photo. The last thing of this is I want those photos uploaded to relate to that user and display on his profile page.
What I have working:
I can upload photos but it goes inside of the regular upload folder, I can also see the filename inside of the database that relates to that photo and I can view the files.
I need the user photos to be created inside of a folder with the username or id. and only the user photos to be displayed on his page. here is my gallery model:
<?php
class Gallery_model extends CI_Model{
// initalizes the gallery path variable
var $gallery_path;
var $gallery_path_url;
public function __construct(){
parent::__construct();
//sets the gallery path to applications folder and gallery image path
$this->gallery_path = realpath(APPPATH . '../portfolio');
$this->gallery_path_url = base_url().'portfolio/';
}
function do_upload(){
$config = array(
// requried variable allowed types is needed also delcared the upload path to gallery path
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 3000
);
// loads the library and sets where its going to go to
$this->load->library('upload',$config);
// this performs the upload operation
$this->upload->do_upload();
//returns data about upload ( file location )
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ration' => true,
'width' => 250,
'height' => 220
);
$data = array(
'username' => $this->input->post('username'),
'category' => $this->input->post('category'),
'filename' => $image_data['file_name'],
);
$this->db->insert('portfolio', $data );
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
function get_images(){
$files = scandir($this->gallery_path);
// substracts these out of array
$files = array_diff($files, array('.', '..','thumbs'));
$images = array();
foreach ($files as $file){
$images [] = array(
'url' => $this->gallery_path_url . $file,
'thumb_url' => $this->gallery_path_url . 'thumbs/' .$file
);
}
return $images;
}
}
and here is my view:
function gallery(){
$this->load->model('user_model');
$data = array(
//$session_id = $this->session->userdata('session_id')
); // if no data is there and you wont get an error
if($query = $this->user_model->get_profile())
{
$data['records'] = $query;
}
// loads the model. if the upload posts, call the do model method from the gallery model Model.
$this->load->model('gallery_model');
if ($this->input->post('upload')){
$this->gallery_model->do_upload();
}
// displays the images
$data['images'] = $this->gallery_model->get_images();
$data['main_content'] = '/pages/gallery';
$this->load->view('templates/template',$data);
}
You see the original filename in the portfolio folder because you missed out the "file_name" config variable for the upload library. Example:
$config = array(
// requried variable allowed types is needed also delcared the upload path to gallery path
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path,
'max_size' => 3000,
'file_name' => "the user id or username"
);
I also see that you do not have "overwrite" variable set. Can be an problem because you are storing the filename as username as CI will not overwrite the file when the user upload a new photo.

Categories