PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] File upload Codeigniter and ajax

 March 05, 2022     codeigniter, jquery, php     No comments   

Issue

I'm not sure what I'm doing wrong here.

controller

function do_test()
{
    $config['upload_path'] = './images';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '10000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['overwrite'] = TRUE;
    $confit['remove_spaces'] = TRUE;

    $this->load->library('upload', $config);
    $this->load->library('form_validation');

    $this->form_validation->set_rules('file', 'trim|required|xss_clean');
    $test = $this->input->post('file');
    var_dump($test); //var dump return : string(21) "C:\fakepath\truth.jpg"

    if ( ! $this->upload->do_upload($test))
    {
        echo "no";
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        echo "yes";
    }
}

form

<?php echo form_open_multipart('home/do_test', array( 'id' => 'skill-form-test' ) );?>

    <input type="file" name="userfile" id="file" size="20" />

    <br /><br />

    <input type="submit" value="upload" />

    </form>

jquery

$('#skill-form-test').submit(function(e) {
    e.preventDefault();

    $.post(base_url + "index.php/home/do_test", { file : $("#file").val()  }, function(data)
        {
            }, "json");
    alert ( $("#file").val() + "-" + base_url ); //This alerts: C:\fakepath\truth.jpg-http://siteurl.com/
});

Solution

Update: The fix is for CodeIgniter v3 and may not work on the recent version.

Just change

if ( ! $this->upload->do_upload($test))

to

if ( ! $this->upload->do_upload('userfile'))

will fix the issue.

Hope this helps you. Thank you!!



Answered By - Madan Sapkota
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing