Getting attachments from http post request in Codeigniter - php

I'm using Mailgun to receive emails for my web app developed using Codeigniter. Mailgun makes a http post to the following (http://something.com/email) url for every incoming email.
I get the contents of the email with the following code.
function email_to_db(){
$email_body = $this->input->post('body-plain', '');
$email_sender = $this->input->post('sender');
}
However I'm not able to fetch the attachments.
Can someone guide me on how I can achieve this. Following is the link to my reference doc.

attachments are files, you should use File Uploading Class
$config = [];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
you can also do print_r($_FILES); and check the attachment files.

Related

get file name after $config['encrypt_name'] in codeigniter

You have added $ config ['encrypt_name'] = TRUE; For the upload code because some user images are called spaces and problems causing problems lifting
After uploading the file its name is changed but I try to take the file name to send it to the database
this way
$ img1 = $ _FILES ['img1'] ['name'];
But I get the original file name before changing
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
If you loop trough each photo and initialize the library with the $_FILES['file_name'] this will help you
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload('file_name');
$image_uploaded = $this->upload->data();
$filename = pathinfo($image_uploaded['full_path']);
$insert['photo'] = $filename['basename'];
$this->db->insert('your_table', $insert);
Don't fall back to the $_FILE superglobal. Since you're using CI, stick to CI's upload wrapper
Assign the output of the upload process to a variable like this:
$your_variable = $this->upload->data();
By doing this, you get all the upload data in your newly created $your_variable array:
$your_variable['file_name'] // encrypted name
$your_variable['file_type'] // MIME type such as image/jpeg or application/pdf
$your_variable['file_path'] // self explanatory
$your_variable['raw_name'] // encrypted name without the extension
$your_variable['orig_name'] // original name
$your_variable['client_name'] // name the file had on the client's computer
$your_variable['file_ext'] // self explanatory
$your_variable['file_size'] // self explanatory. Size in Kb
$your_variable['is_image'] // 1 if yes, 0 otherwise
$your_variable['image_width'] // only for images
$your_variable['image_height'] // only for images
$your_variable['image_type'] // only for images
$your_variable['image_size_str'] // only for images
I'm pretty sure those are all you get as of CI 3.1.10 (but I may have forgotten a couple, if so I apologize)
You need the ['file_name'] element for your purposes.
Going back to the $_FILE superglobal will not work, as CI won't modify its values

Codeigniter Upload function does not work

I am working on codeigniter and during creation of one of the APIs I got the issue. I tried to upload the image on Server as a file, while searching on the web, I got familiar with inbuild upload class in codeigniter. Please have a look at this code. I am sending file from Android using this tutorial.
public function upload_image_post(){
$config['upload_path'] =base_url().'/uploads/';
$config['file_name'] = rand() .'.jpg';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 10000;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = TRUE;
// $file = $this->input->post('file');
$this->load->library('upload', $config);
// $this->upload->initialize($config);
$file=$_FILES['uploaded_file'];
// $this->upload->do_upload($file);
if($file){
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'asQ',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is working*/
$res = $this->db->insert('ww_portfolio_images',$content);
}else{
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'not file',
'sp_id'=>'asQaaaaa',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is not working, Thats Obvious*/
$res = $this->db->insert('ww_portfolio_images',$content);
}
// $destinationPath=APPPATH.'public/assets/uploads/ANKO.jpg';
if($this->upload->do_upload('uploaded_file')) {
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'aaaaaaaaaaaaaaaaas',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
/* This is not working*/
$res = $this->db->insert('ww_portfolio_images',$content);
$this->response(['result' =>'Success',] , REST_Controller::HTTP_OK);
// return ($arr_image_info['full_path']);
}
else{
$content=array(
'image_id'=>'IMG'.rand(),
'album_id'=> 'A',
'sp_id'=>'asass',
'image_name'=>'aAA',
'status'=>1,
'tags'=>'s');
$res = $this->db->insert('ww_portfolio_images',$content);
/* This is working*/
$this->response(['result' => 'ERrro'] , REST_Controller::HTTP_OK);
// $this->response(['result' =>'Image error',], 433);
}
}
I can not figure out the problem I am facing here. I am receiving a file but it does not upload.
I have also tried to use $this->upload->do_upload() instead of $this->upload->do_upload('uploaded_file') and this $config['max_size'] = '10000'; instead of this $config['max_size'] = 10000; . Please help. Any help would be greatly appreciated.
Also, when this code run through web panel, it working fine.
Better if you provide some more detail for the type of error or warning that you are observing.
There could be number of reasons.
1) base_url() gives you the public URL. You have to specify the absolute or relative path to your upload folder.
2) (if you are using an Apache Server) Your Apache user don't have the write permission to the upload folder.
3) Folder path doesn't exists.
If the first one doesn't work, please check the rest of the points. Hope this help you.
Regards
Muaaz
Try using FCPATH
$config['upload_path'] = FCPATH . '/uploads/';
Or
$config['upload_path'] = './uploads/';
Then http://www.codeigniter.com/user_guide/libraries/file_uploading.html#the-controller
<?php
class Example extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('form');
}
// Name function what every you want remember to change it on view form.
public function upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ($this->upload->do_upload('uploaded_file')) {
// Then the success stuff
$upload_data = $this->upload->data();
echo $upload_data['file_name'];
} else {
// Errors
}
}
}
On view I would use the form helper functions form_open_multipart()
<?php echo form_open_multipart('example/upload');?>
<?php echo form_upload('uploaded_file', 'Upload');?>
<?php echo form_close();?>
check your form input in the view
My form view :
<input id="document" type="file" data-browse-label="browse" name="document" data-show-upload="false" data-show-preview="false" class="form-control file" />
other can be
permission issue for the uploads folder
if the folder does not exist you have to create it
And always try to debug the code with logs
document in the do_upload is the name of the input element in my view
if ($_FILES['document']['size'] > 0) {
$this->load->library('upload');
$config['upload_path'] = 'uploads/images';
$config['allowed_types'] = '*';
$config['max_size'] = $this->allowed_file_size;
$config['overwrite'] = false;
$config['encrypt_name'] = true;
$this->upload->initialize($config);
if (!$this->upload->do_upload('document')) {
$error = $this->upload->display_errors();
$this->session->set_flashdata('error', $error);
redirect($_SERVER["HTTP_REFERER"]);
}
$photo = $this->upload->file_name;
}

I can't upload a markdown file by the CodeIgniter

I don't know how to set the allowed_types, and the markdown file suffix is .md. It always says:
The filetype you are attempting to upload is not allowed.
English is not my native language, so I hope this makes sense. My code:
public function do_upload()
{
$config['upload_path'] = './blog/';
$config['allowed_types'] = ''; //file's tye
$config['max_size'] = 50;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('houtai_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
}
You just need to allowed_types in your $config array
$config['allowed_types'] = 'md';
If you want to allow all kinds of files than you can use * like:
$config['allowed_types'] = '*';
File Uploading Class
**Customize File type allow **
$config['allowed_types'] = 'gif|jpg|png';
You can edit APPATH.'config/mimes.php' file and add 'md' => 'text/plain', to array.

Uploading image using Codeigniter (API)

I'm trying to implement a simple image upload function in my API using codeigniter. So far this is my code :
function addpicture_post()
{
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload() )
{
print_r($this->upload->display_errors('', ''));
print_r($this->upload->data());
$info = 'not working';
$this->response($info);
}
}
}
When I'm using apigee (https://apigee.com/console/others) to try it, if I do not set any parameter, I have the following message :
HTTP/1.1 200 OK
Status:
200
Date:
Thu, 12 Dec 2013 15:30:16 GMT
Content-Length:
367
Keep-Alive:
timeout=15, max=100
Content-Type:
application/json
X-Powered-By:
PHP/5.3.5-1ubuntu7.11
Server:
Apache/2.2.17 (Ubuntu)
You did not select a file to upload.
So far so good. But when I'm trying to upload an image file (with the file attachement parameter in apogee, which I named "userfile"), I have the following response :
Invalid test url
I'm fairly struggling with that for a few hours now, I've tried to look at code igniter's documentation but unfortunately it has nothing to do with APIs , and I can't figure out how to upload files without using a form.
I'm not quite sure if the problem is coming from the service I'm using to test it, apigee, or if the problem is coming from the code. At the end, my goal is to implement this function in an iphone app I'm coding, but I'd like to be able to test the function before trying to include it in my app.
Anyone could give me informations about file upload within an API ? And how to test it properly ? Thank you.
Try This, it works!
*input file name = 'image'
function upload_image(){
$img = array(); // return variable
$this->load->helper(array('file','directory'));
if (!empty($collection)) {
$path="./uploads/";
if( !is_dir($path) ) {
mkdir($path);
}
$config['upload_path'] = $path; /* NB! create this dir! */
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['file_name'] = 'image001';
$config['overwrite']=TRUE;
$this->load->library('upload', $config);
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = FALSE;
$configThumb['maintain_ratio'] = FALSE;
/* Load the image library */
$this->load->library('image_lib');
/* We have 5 files to upload
* If you want more - change the 6 below as needed
*/
/* Handle the file upload */
if (isset($_FILES['image']['tmp_name'])) {
$upload = $this->upload->do_upload('image');
/* File failed to upload - continue */
if($upload === FALSE){
$error = array('error' => $this->upload->display_errors());
$data['message']=$error['error'];
continue;
}
/* Get the data about the file */
$data = $this->upload->data();
$img['image']='/'.$data['file_name'];
}
}
return $img;
}
Try this, it might help you, I did this using codeigniter frame work, here is the controller
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 8000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
here is a view file
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<input type="submit" value="upload" />
Its simple in codeigniter you can use this method also , take data from post
method=multiform/form-data
And use this function for uploading the file by changing its name using md5 and store it in db
$md5 = md5(date('d-m-Y H:i:s')); //md5 the constant here i have choosen date()
if(!empty($_FILES['profile_pik']['name'])) //check whether file is there or not
{
$moveprofilepik='assets/uploads/profile_pik/' . $md5.str_replace(' ', '', $_FILES['d_profile_pik']['name']); //append the location where you want to move and also concat the name using md5 value
move_uploaded_file($_FILES['d_profile_pik']['tmp_name'], $move1); //finally use inbuilt function move_uploaded_file and pass values
}

codeigniter file upload

I am doing the following to upload a file(image) using codeigniter. What I am wanting to do is modify the error message so that it obvious which field the error relates too as there are multiple upload options on the page, below is my upload code,
$config['upload_path'] = './media/uploads/users';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '90';
$config['max_height'] = '60';
$this->upload->initialize($config);
if(!$this->upload->do_upload('logo_small'))
{
$error = array('error' => $this->upload->display_errors());
$this->template->build('users/employer', $error);
}
else
{
$file = $this->upload->data();
$logo_small = "small_".$file['file_name'];
}
So basically I want there error message to state Logo Small in the error message, and then if the error relates to Logo large etc I would want it to state that.
easiest way is to edit system/language/english/upload_lang.php with the error message you want.

Categories