How can i upload image with ajax in codeigniter?

前端 未结 4 787
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 06:46

I\'m trying to upload image with jQuery and ajax function, and how can I fetch all details of image file, like in php we use $_FILE()

Here is my code

相关标签:
4条回答
  • 2020-12-16 07:04

    In the view where you are displaying form:

    add attribute: enctype=multipart/form-data

    Or,

    If you are creating a form with form helper:

    <?php echo form_open_multipart('blog/uploadimg');?>
    
    0 讨论(0)
  • 2020-12-16 07:10

    You can use Ajaxfileupload.js to upload file:

    $('input[type="file"]').ajaxfileupload({
              'action': 'save_photo.php',
              'params': {
                'extra': 'info'
              },
              'onComplete': function(response) {
    
               console.log('custom handler for file:');
               alert(JSON.stringify(response));
    
              },
              'onStart': function() {               
    
              },
              'onCancel': function() {
                console.log('no file selected');
              }
        });
    

    save_photo.php :

    <?php
    
    print_r($_FILES); // print details about the file which has been uploaded
    
    ?>
    
    0 讨论(0)
  • 2020-12-16 07:21

    You can get seamless functionality if you use this plugin (well written) in javascript.

    http://malsup.com/jquery/form/

    To Upload File in PHP

    <?php
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["file"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["file"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
    ?>
    
    0 讨论(0)
  • 2020-12-16 07:24

    You can to use FormData api in html5.

    Your form must be:

    <form enctype="multipart/form-data" accept-charset="utf-8" name="formname" id="formname"  method="post" action="">
    

    Then jquery:

    function uploadImage() {
    
        if (typeof FormData !== 'undefined') {
    
            // send the formData
            var formData = new FormData( $("#formID")[0] );
    
            $.ajax({
                url : baseUrl + 'uploadImage',  // Controller URL
                type : 'POST',
                data : formData,
                async : false,
                cache : false,
                contentType : false,
                processData : false,
                success : function(data) {
                    successFunction(data);
                }
            });
    
        } else {
           message("Your Browser Don't support FormData API! Use IE 10 or Above!");
        }   
    }
    

    Then in the controller you will get the files in $_FILES array.

    0 讨论(0)
提交回复
热议问题