Codeigniter Upload File Issue - codeigniter

Hi this is my code:
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/licenta/application/images/products';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$x = $this->load->library('upload', $config);
if (!$this->upload->do_upload()){
//failed
}else{
//success
}
This is: $this->upload->do_upload() always false. My form is correct I get the right data if I use var_dump($_FILES);. The path is:
C:/xampp/htdocs/licenta/application/images/products
I tried also this version /images/products/ or ./images/products/ or ../images/product/. None of them are working. I am using XAMPP. What else should be the problem? I check the others post and the documentation and for them are working, for me is not. Can someone help me? I am stuck :(

Just pass the HTML field name attribute in do_upload() function:
See below code:
$this->upload->do_upload('userfile');
I hope it will work.

Related

CodeIgniter upload txt doc - The filetype you are attempting to upload is not allowed

CodeIngiter version: 2.2
I'm developing a PHP API and I'm trying to upload a file. The code is:
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png|jpeg|jpe|pdf|doc|docx|rtf|text|txt';
$config['max_size'] = '4096';
$config['max_width'] = '1024';
$config['max_height'] = '1024';
$config['encrypt_name'] = true;
$CI =& get_instance();
$CI->load->library('upload', $config);
$CI->upload->do_upload("file")
It works with image files and pdf files, but when I try to upload a .txt o .doc file I got the error: filetype you are attempting to upload is not allowed
I'm doing the test with Postman tool (form-data mode). I have checked the mimes.php file and it seems right.
Can anyone help me? Thanks
Did you check the file_type that your server is actually receiving?
var_dump($this->upload->data());
Then you can check if your mimes is really right.
hope it helps
#edit
it seems that you are trying to upload an empty file. If you need your app to accept even empty files, you can do this in mimes.php (line 'txt'):
'txt' => array('text/plain', 'application/x-empty'),

How to get file data before upload in codeigniter

I want to get file data before upload happens. I have tried this with the following command order
$this->upload->get_data()
$this->upload->do_upload()
but this gave empty results. (It works the other way round)
So my question is: how is possible to get file data in codeigniter before upload happens?
var_dump($_FILES); die; OR
$upload_data = $this->upload->data();
var_dump($upload_data); die;
Your file is not being uploading
you need to check this
$error = $this->upload->display_errors();
print_r($error);
and one thing more
$this->upload->do_upload('input_filed_name');

Codeigniter putting underscore when overwrite is true in upload method

$config['file_name'] = $logged_in_user['user_name'].".png";
$config['upload_path'] = 'assets/profiles/';
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = true;
$this->load->library('upload', $config);
if ($this->upload->do_upload("picture")) {
print_r($this->upload->data());
$upload_data = $this->upload->data();
}
When i am running this function i have already a file abc.png in my directory. As the upload methods runs, it is saving a file with abc_.png Why it is appending underscore? I read in other post here that this could be because of spaces. But i dont think there is any space in $config['file_name'] = $logged_in_user['user_name'].".png";
Please note that the already existed abc.png is pure PNG image while the file i am uploading is actually a jpeg image which i concatenate with .png just to make the file name same. I will do image conversion later after fixing this issue.
Thanks
Try doing this to make sure there are no spaces:
Change
$config['file_name'] = $logged_in_user['user_name'].".png";
To
$config['file_name'] = trim($logged_in_user['user_name']).".png";

html is pass using jquery ajax but pdf is not generating in mpdf for codeigniter

I want to create a PDF in codeigniter using mPDF. My html is passed to the controller using jQuery AJAX. Data is coming to the $html But it is not working. It works fine when html is hard coded. Can any one help me please?
public function pdf($paper='A4')
{
$html = '';
$html = $this->input->POST('content');
$this->load->library('mpdf54/mpdf');
$CI->mpdf = new mPDF('utf-8',$paper);
$mpdf->debug = true;
$this->mpdf->WriteHTML($html);
$this->mpdf->Output();
exit;
}
Try grabbing all the POST vars by using
$html = $this->input->POST();
then echo those out to yourself before moving farther to be sure they are getting set.
public function pdf($paper='A4')
{
print_r($this->input->POST());
return;
}
This of course is only for testing but might help you to see why your $html var isn't getting set. Try that out and give us the results.

Codeigniter: URL solutionless problem

I have following url:
http://localhost.com/phpdemo/bid/tf/red?
This url redirects through This [ $route['tf/red?'] = "abc/blue" ] to following url:
http://localhost.com/phpdemo/bid/abc/blue
Till now there is no problem. The problem starts when I attach some value with "?" like below:
http://localhost.com/phpdemo/bid/tf/red?a [It always go to default welcome page]
I have tried follwoing routes:
$route['tf/red?(:any)'] = "abc/blue"
$route['tf/red?:any'] = "abc/blue"
$route['tf/red?(a-zA-Z0-9=)'] = "abc/blue"
I have tried following config settings:
$config['permitted_uri_chars'] = 'a-z A-Z 0-9~%.:_\-';
$config['enable_query_strings'] = FALSE;
$config['allow_get_array'] = TRUE;
I also checked by using following:
$config['enable_query_strings'] = TRUE;
Now Iam clueless, what is wrong, either with Codeigniter or myself.
Can some one guide me in this regards.
Thanks in advance
I would look at the value of $config['uri_protocol'] - it is set in the main config.php file and the default is 'AUTO'.
Try each of the possible values to see which works for you - PATH_INFO or REQUEST_URI are common choices.

Resources