Uploading files in codeigniter - php

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

Related

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

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/';

Form not uploading file in codeigniter 2.0

I have tried every possible method but no success. Just trying to upload file using code igniter but not working the error I am getting
<pre>Array
(
[error] => <p>You did not select a file to upload.</p>
)
I have tried in normal core php at my local host that works fine but not working with code igniter. It is simply not picking the file. If I check with var_dump($_FILES['fileToUpload']); the result will be array(0).
Form Code
<form id="contact_form" enctype="multipart/form-data" method="post" action="<?php echo base_url();?>Main/do_upload">
<input type="file" class="form-control" name="fileToUpload" id="fileToUpload">
</form>
Controller Code
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'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"
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
echo "<pre>";
var_dump($data);
// $this->load->view('upload_success',$data);
}else{
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
}
config
$autoload['libraries'] = array("session", "email", "database");
$autoload['helper'] = array("url", "file", "form");
Is there anything I am not aware of ? Please guide I am stuck here.
You missed input file name in do_upload():
Use :
if(!$this->upload->do_upload('image_file'))
{
//$this->upload->display_errors()
}
else
{
//$this->upload->data()
}
Instead of:
if($this->upload->do_upload())
You miss parameters in $this->upload->do_upload Pleas check below code.
public function do_upload(){
$config = array(
'upload_path' => "assets/uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'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"
);
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('fileToUpload'))
{
$data = array('upload_data' => $this->upload->data());
echo "<pre>";
var_dump($data);
// $this->load->view('upload_success',$data);
}else{
$error = array('error' => $this->upload->display_errors());
echo "<pre>";
print_r($error);
}
}
Pass your file upload name in $this->upload->do_upload('fileToUpload')

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",
);

Cant Upload doc format files in codeigniter

$config['allowed_types'] = 'doc|Doc';
I want to upload Doc format file only but cant upload doc format file just written The filetype you are attempting to upload is not allowed.
Why?
How to solve issue,
I add mimes file,
'doc' => 'application/msword',
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip'),
you can use this function
public function do_upload(){
$this->config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/files/",
'upload_url' => base_url()."files/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml|docx|GIF|JPG|PNG|JPEG|PDF|DOC|XML|DOCX|xls|xlsx",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
$this->remove_dir($this->config["upload_path"], false);
$this->ci->load->library('upload', $this->config);
if($this->ci->upload->do_upload())
{
$this->ci->data['status']->message = "File Uploaded Successfully";
$this->ci->data['status']->success = TRUE;
$this->ci->data["uploaded_file"] = $this->ci->upload->data();
}
else
{
$this->ci->data['status']->message = $this->ci->upload->display_errors();
$this->ci->data['status']->success = FALSE;
}
}

codeigniter not showing file upload error

I am using CodeIgniter file uploading class to upload a image. But if I try and select a pdf instead of image I do not get any errors on form submission.
relevant code
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->article_path.'/magazine',
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$this->upload->display_errors();
$image_data = $this->upload->data();
It is because you never went to the documentation (here), please do so in order to perform better in CodeIgniter framework.
this should give you heads up (please read comments)
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->article_path.'/magazine',
'max_size' => 2000
);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) //important!
{
// something went really wrong show error page
$error = array('error' => $this->upload->display_errors()); //associate view variable $error with upload errors
$this->load->view('upload_form', $error); //show error page
}
else
{
//all is good we upload
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}

Categories