上传图片并在Codeigniter中显示

I have a form to upload a picture like below :

<head>
    <title> Image Upload </title>
</head>

<body>
    <div id="container">
        <?php echo  form_open_multipart('upload/uploadImage')?>
        <input type="file" name="userfile" />
        <p>
            <input type="submit" name="submit" value="submit" />
        </p>
        <?php echo form_close();?>
    </div>
</body>

</html>

How can I show a display of choose file in the form?

Seems you need javascript for this:

<div class="col-lg-6">
                <input id="userfile" name="userfile" type="file"  onchange="showMyImage(this)">
                <img id="dummyImage" src="#" alt="your image" style="display: none; width: 200px;"  />
            </div>

<script>
    function showMyImage(fileInput) {
        var files = fileInput.files;
        $('#dummyImage').show();
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            var imageType = /image.*/;
            if (!file.type.match(imageType)) {
                continue;
            }
            var img = document.getElementById("dummyImage");
            img.file = file;
            var reader = new FileReader();
            reader.onload = (function (aImg) {
                return function (e) {
                    aImg.src = e.target.result;
                };
            })(img);
            reader.readAsDataURL(file);
        }
    }
</script>

You can also call the showMyImage function on document.ready

You can do that using the javascript FileReader(), example below:

function readURL(input) {
if (input.files && input.files[0]) {
    var reader = new FileReader();
    reader.onload = function (e) {
        $('.image_preview').html('<img src="'+e.target.result+'" alt="'+reader.name+'" class="img-thumbnail" width="304" height="236"/>');
    }
    reader.readAsDataURL(input.files[0]);
}
}

Use the function to your like this - I also have a github repo for this using codeigniter, check the link below:

https://github.com/adrianrios25/CI_upload

I hope this will help you.