codeigniter not showing file upload error - php

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);
}

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')

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

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
}

File name not grabbed when using CodeIgniter upload library

Firstly can I request this is NOT marked as duplicate. I have read the other posts on SO regarding issues with the CodeIgniter upload library and sadly they do not cover this. I have also extensively read the CI documentation and everything suggests this should work correctly.
I am using a very simple form to grab the file, which is uploaded correctly to the images folder. The full_path of the file is also written successfully to a db table called images. The filename however is blank.
My form:
<?php echo form_open_multipart('image_upload/do_upload');?>
<input type="file" name="userfile" size="20" multiple="true" />
<br /><br />
<input type="submit" value="upload" />
</form>
My Controller function:
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$image_data = $this->upload->data();
echo '<pre>'; print_r($image_data); echo '</pre>';
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
I am using
echo print_r($image_data); echo;
To display the associated image data but this is all that is returned:
Array
(
[file_name] =>
[file_type] =>
[file_path] => c:/wamp/www/honest/images/
[full_path] => c:/wamp/www/honest/images/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
I can't work out why it is not grabbing the file name and other details - can someone help spot what is hopefully a simple error?
Many thanks,
DP.
You are requesting your uploaded data before you actually upload anything. That's why your $image_data only contains your configuration values. Move your $image_data = $this->upload->data(); call to after actually performing the do_upload() (into your else block):
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$image_data = $this->upload->data();
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}

Categories