Dany Marsh
@dany.marsh
Php Developer, Blogger

Dany's professions

Recent Posts By Dany

Upload file, image using curl in codeigniter with ajax call
This is tutorial regarding upload image on third party server using curl in codeigniter php framework also used ajax call to upload.
View file code looks like :
<form name="file_upload_form" id="file_upload_form" method="post" action="" enctype="multipart/form-data" onsubmit="return false;">
<input type="file" name="file_upload" id="file_upload" class="file-input" />
<input type="submit" name="file" id="file_save" value="Upload File"> 
</form>
Jquery code :

$(document).on("click", "file_save", function () {
    formData = new FormData(document.forms.namedItem("file_upload_form"));
    $.ajax({
        url: base_url + '/user/upload_file',
        type: 'POST',
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        dataType: 'json',
        success: function (data) {
            if (data.Result == "Success") {
                console.log("File uploaded successfully");
            } else {
                console.log("Server problem try again");
            }

        }
    });
});
User Controller code:
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('uploadFiles');
    }

    public function upload_file() {
        $file = $_FILES['file'];
        $data = array('apikey' => 'key',
            'mode' => 'document_photo',
            'file' => new CurlFile($file['tmp_name']));
        $output = $this->uploadFiles->upload_userdata($data);
        echo json_encode(array("Result" => "Success", "msg" => $output));
    }

}

?>
Create Library for CURL call this lib contains reusable code
 uploadFiles.php lib code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class uploadFiles {

    private $CI_instance;  // CI Instance created
    private $curl_base_url; //This is curl base url where you store your images[Server path]

    public function __construct() {

        $this->CI_instance = & get_instance();
        $this->curl_base_url = $this->CI_instance->config->item('curl_base_url'); //Actual path is stored in config/config.php file
    }

    public function upload_userdata($data, $url = "your server file name") {
        $content = $data;
        $url = $this->curl_base_url . $url;
        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_URL, $url);
        curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
        curl_setopt($curl_handle, CURLOPT_POST, 1);
        curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $content);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($curl_handle);
        $status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
        if ($status != 200) {
            die("Error: call to URL $url failed with status $status, curl_error " . curl_error($curl_handle) . ", curl_errno " . curl_errno($curl_handle));
        }
        curl_close($curl_handle);
        return json_decode($output);
    }

}
and this works perfectly :), this might be helpful for you , Thank you :)
×

Jon Westenberg follows

  • John Marshal
    @john
    Writer, founder, passionate entrepreneur + I'm on a mission to build businesses
  • John Marshal
    @john
    Writer, founder, passionate entrepreneur + I'm on a mission to build businesses
  • John Marshal
    @john
    Writer, founder, passionate entrepreneur + I'm on a mission to build businesses
  • John Marshal
    @john
    Writer, founder, passionate entrepreneur + I'm on a mission to build businesses
  • John Marshal
    @john
    Writer, founder, passionate entrepreneur + I'm on a mission to build businesses
×