如何在php codeigniter中显示来自url的图像

时间:2017-06-08 06:03:16

标签: php codeigniter

 In controller:
public function submit_image() {
    $post = $this->input->post ();
    $this->common->submit_image ( $_FILES, $post );
    $this->session->set_flashdata ( 'success', "alert came" );
    redirect ( BASE_URL . 'admin/media_mgmt/' );
}  

In model:
function submit_image($file, $data) {
    if ($fname = is_valid_val ( $data, 'name', FALSE )) {
        $this->db->set ( 'name', $this->db->escape_str ( htmlspecialchars ( $fname ) ) );
    }
    if ($fname = is_valid_val ( $data, 'tag', FALSE )) {
        $this->db->set ( 'tag', $this->db->escape_str ( htmlspecialchars ( $fname ) ) );
    }
    $name = upload_media_image ( $file );

    if ($name) {
        $data1 ['media_img'] =  BASE_PATH . MEDIA_PHOTO . '/' . $name;
        $this->db->set ( 'image', $this->db->escape_str ( htmlspecialchars ( $data1 ['media_img'] ) ) );
    }

    if ($id = is_valid_val ( $data, 'id', FALSE )) {
        $this->db->where ( 'id', $this->db->escape_str ( htmlspecialchars ( $id ) ) );
        $this->db->update ( TBL_MEDIA_PHOTO );
        if ($this->db->affected_rows () > 0) {
            return 1;
        } else {
            return FALSE;
        }
    } else {
        $this->db->insert ( TBL_MEDIA_PHOTO );
        if ($id = $this->db->insert_id ()) {
            return 1;
        }
    }
}


In php:
var image_list = $('#demo-foo-filtering').DataTable( {

     "processing": true,
     "serverSide": true,
     "ajax": {
         "url": "<?php echo BASE_URL;?>admin/ajax_image_list",
         "type": "POST"
     },
     "columns": [
         { "data": "id" },
         { "data": "name" },
         { "data": "tag" },
         { "data": "image" },        

     ],

 } );
 $('#image_form').parsley().on('form:success', function() {
     var formdata = $('#image_form').serialize();
            $.ajax({
                url:"<?php echo BASE_URL.'admin/submit_image';?>",
                type: 'POST',
                data: formdata,
                success: function(res) {
                        if(res==1){
                            Custombox.close();
                            $('#success_edit').css("display",'block');
                            $('#success_edit').fadeIn().delay(3000).fadeOut();
                        }else{
                            //$('#error_edit').css("display",'block');

                        }                       
                }
            })
     });

这是我的控制器模型和php代码。我想只显示管理面板中的图像。现在图像来了&#34; http://localhost/edudux_web/manage/media_photo//4.jpg&#34;从数据库获取时而不是网址我想在面板上显示实际图像

2 个答案:

答案 0 :(得分:0)

您必须在ajax响应和控制器中传递图像URL:

在控制器中:

redirect ( BASE_URL . 'admin/media_mgmt/' ); 

将其更改为返回:

 public function submit_image() {
    $post = $this->input->post ();
    $name = $this->common->submit_image ( $_FILES, $post );
    $this->session->set_flashdata ( 'success', "alert came" );
    return BASE_URL . 'admin/media_mgmt/'.$name;
} 

在php中:

if(res){
   Custombox.close();
   $('#success_edit').html('<img src="' + res + '" />'); }
   $('#success_edit').css("display",'block');
   $('#success_edit').fadeIn().delay(3000).fadeOut();
  }else{
     //$('#error_edit').css("display",'block');
  }
模特中的

模特:

function submit_image($file, $data) {
    if ($fname = is_valid_val ( $data, 'name', FALSE )) {
        $this->db->set ( 'name', $this->db->escape_str ( htmlspecialchars ( $fname ) ) );
    }
    if ($fname = is_valid_val ( $data, 'tag', FALSE )) {
        $this->db->set ( 'tag', $this->db->escape_str ( htmlspecialchars ( $fname ) ) );
    }
    $name = upload_media_image ( $file );

    if ($name) {
        $data1 ['media_img'] =  BASE_PATH . MEDIA_PHOTO . '/' . $name;
        $this->db->set ( 'image', $this->db->escape_str ( htmlspecialchars ( $data1 ['media_img'] ) ) );
    }

    if ($id = is_valid_val ( $data, 'id', FALSE )) {
        $this->db->where ( 'id', $this->db->escape_str ( htmlspecialchars ( $id ) ) );
        $this->db->update ( TBL_MEDIA_PHOTO );
        if ($this->db->affected_rows () > 0) {
            return $name;
        } else {
            return FALSE;
        }
    } else {
        $this->db->insert ( TBL_MEDIA_PHOTO );
        if ($id = $this->db->insert_id ()) {
            return $name;
        }
    }
}

答案 1 :(得分:0)

  <!-- ============================================================== -->
<!-- Start right Content here -->
<!-- ============================================================== -->
         <div class="content-page">
<!-- Start content -->
       <div class="content">
    <div class="container">

        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">


                <h4 class="page-title">Gallery</h4>
                <ol class="breadcrumb">

                </ol>
            </div>
        </div>


        <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 ">
                <div class="portfolioFilter">
                    <a href="#" data-filter="*" class="current" 
            onclick="showall()">All</a>
                    <a href="#" data-filter=".webdesign" 
              onclick="showimgbutton()">Images</a>
                    <a href="#" data-filter=".graphicdesign"
                        onclick="showvideobutton()">Videos</a>
                    <button id="imga"
                        class="btn btn-default btn-md waves-effect waves-
  light "
                        data-toggle="modal" data-target="#myModal"
                        style="visibility: hidden">
                        <i class="md md-add"></i> Add Image
                    </button>
                    <a id="imgv" href="#custom-modal1"
                        class="btn btn-default btn-md waves-effect waves-
                         light "
                        data-animation="fadein" data-plugin="custommodal"
                        data-overlaySpeed="200" data-overlayColor="#36404a"
                        style="visibility: hidden"> <i class="md md-add">
             </i> Add Video
                    </a>
                </div>
            </div>
        </div>
        <div role="alert"
            class="alert alert-success alert-dismissible fade in"
            id="success_edit" style="display: none;">
            <button aria-label="Close" data-dismiss="alert" class="close"
                type="button">
                    <span aria-hidden="true">x</span>
            </button>
            <strong>Hey!</strong> Image Added Successfully.
        </div>
        <div role="alert"
            class="alert alert-danger alert-dismissible fade in" 
       id="error_edit"
            style="display: none;">
            <button aria-label="Close" data-dismiss="alert" class="close"
                type="button">
                <span aria-hidden="true">x</span>
            </button>
            <strong>Oops!</strong> Error while Image add.
        </div>
        <div role="alert"
            class="alert alert-success alert-dismissible fade in"
            id="success_edit1" style="display: none;">
            <button aria-label="Close" data-dismiss="alert" class="close"
                type="button">
                <span aria-hidden="true">x</span>
            </button>
            <strong>Hey!</strong> Video Added Successfully.
        </div>
        <div role="alert"
            class="alert alert-danger alert-dismissible fade in"
            id="error_edit1" style="display: none;">
            <button aria-label="Close" data-dismiss="alert" class="close"
                type="button">
                <span aria-hidden="true">x</span>
            </button>
            <strong>Oops!</strong> Error while Video add.
        </div>
        <script type="text/javascript">
            function showimgbutton()
            {
                $('#imga').css("visibility",'visible');
                $('#imgv').css("visibility",'hidden');
            }
            function showvideobutton()
            {
                $('#imga').css("visibility",'hidden');
                $('#imgv').css("visibility",'visible');
            }
            function showall()
            {
                $('#imga').css("visibility",'hidden');
                $('#imgv').css("visibility",'hidden');
            }
        </script>
        <table id="demo-foo-filtering"
            class="table table-striped dataTables_wrapper" data-page-
            size="7">
            <thead>
                <tr>
                    <th>ID</th>
                    <th data-toggle="true">Name</th>
                    <th>Tags</th>
                    <th data-hide="phone">Image</th>

                    <img src="<?php echo BASE_PATH; ?>media_photo/">                                                        

                </tr>
            </thead>
            <tfoot>
            </tfoot>
                </table>
                <!-- <div class="row port">
            <div class="portfolioContainer">
                <div class="col-sm-6 col-lg-3 col-md-4 webdesign 
             illustrator">
                    <div class="gal-detail thumb"><!-- <img src="<?php echo 
             $row['gallery_image'];?>" alt=""> 
                        <a
                            href="<?php echo BASE_PATH;?
        >admin_assets/images/gallery/1.jpg"
                            class="image-popup" title="Screenshot-1">
                                <img
                            src="<?php echo BASE_PATH;?
               >admin_assets/images/gallery/1.jpg"
                            class="thumb-img" alt="work-thumbnail">
                        </a>
                        <h4>School Image1</h4>
                    </div>
                </div>

                <div
                    class="col-sm-6 col-lg-3 col-md-4 webdesign illustrator 
             photography">
                    <div class="gal-detail thumb">
                        <a
                            href="<?php echo BASE_PATH;?
         >admin_assets/images/gallery/2.jpg"
                            class="image-popup" title="Screenshot-2"> 
                            <img
                            src="<?php echo BASE_PATH;?
          >admin_assets/images/gallery/2.jpg"
                            class="thumb-img" alt="work-thumbnail">
                        </a>
                        <h4>School Image2</h4>
                    </div>
                </div>

                <div
                    class="col-sm-6 col-lg-3 col-md-4  webdesign illustrator 
             photography">
                    <div class="gal-detail thumb">
                        <a href="<?php echo BASE_PATH;?
         >assets/images/gallery/3.jpg"
                            class="image-popup" title="Screenshot-3"> <img
                            src="<?php echo BASE_PATH;?
            >admin_assets/images/gallery/3.jpg"
                            class="thumb-img" alt="work-thumbnail">
                        </a>
                        <h4>School Image3</h4>
                    </div>
                </div>-->

        <div class="col-md-4   graphicdesign illustrator photography">
            <div class="card-box gal-detail thumb">
                <h4 class="text-dark header-title m-t-0">Video 1</h4>

                <!-- 16:9 aspect ratio-->

                <div class="embed-responsive embed-responsive-16by9">
                     <iframe height="100%" width="100%"
                    src="https://www.youtube.com/embed/">
                </iframe> 
                </div>
            </div>
        </div>
        <div class="col-md-4   graphicdesign illustrator photography">
            <div class="card-box gal-detail thumb">
                <h4 class="text-dark header-title m-t-0">Video 2</h4>

                <!-- 16:9 aspect ratio-->

                <div class="embed-responsive embed-responsive-16by9">
            <!--         <iframe height="100%" width="100%"
                    src="https://www.youtube.com/embed/kwkkkuAf19g">
                </iframe>  -->
                </div>
            </div>
        </div>

    </div>
    </div>
    <!-- End row -->

       </div>
      <!-- container -->

           </div>
          <!-- content -->
              </div>
        <div id="myModal" class="modal fade" tabindex="-1" role="dialog"
      aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"
                aria-hidden="true">
                <span>&times;</span><span class="sr-only">Close</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Add Image</h4>
        </div>
        <div class="modal-body">
            <form id="image_form" class="form-horizontal" role="form"
                data-parsley-validate novalidate enctype="multipart/form-
           data"
                method="post" action="<?php echo 
               BASE_URL.'admin/submit_image'?>">
                <div class="form-group">
                    <label for="userName" class="col-sm-4 control-
                   label">Name*</label>
                    <div class="col-sm-7">
                        <input type="text" name="name" data-parsley-
        trigger="change"
                            required placeholder="" class="form-control" 
     id="name">
                    </div>
                </div>

                <div class="form-group">
                    <label for="userName" class="col-sm-4 control-
            label">Tag</label>
                    <div class="col-sm-8">
                        <input type="text" name="tag" size="8" 
                 placeholder=""
                            class="form-control" id="tag" data-
           role="tagsinput">
                        <!-- <textarea class="form-control" rows="5" data-
               role="tagsinput"></textarea> -->
                    </div>
                </div>
                <div class="form-group">
                    <label for="userName" class="col-sm-4 control-
              label">Upload Image</label>
                    <div class="col-sm-7">
                        <input type="file" class="filestyle" required 
                   name="media_img"
                            id="media_img" data-iconname="fa fa-cloud-
                 upload">

                    </div>
                </div>


                <div class="form-group">
                    <div class="col-sm-offset-4 col-sm-8">
                        <button type="submit" id="submit_image"
                            class="btn btn-primary waves-effect waves-
               light">Submit</button>
                        <button type="reset" 
              onclick="$('#myModal').modal('hide');"
                            class="btn btn-default waves-effect waves-light 
               m-l-5">Cancel</button>
                    </div>
                </div>
            </form>
        </div>

    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
            </div>
                 <!-- Modal -->
        <div id="custom-modal1" class="modal-demo">
  <button type="button" class="close" onclick="Custombox.close();">
    <span>&times;</span><span class="sr-only">Close</span>
  </button>
  <h4 class="custom-modal-title">Add Video</h4>
  <div class="custom-modal-text text-left">
    <form id="video_form" class="form-horizontal" role="form"
        data-parsley-validate novalidate
        action="<?php echo BASE_URL.'admin/submit_video'?>">
        <div class="form-group">
            <label for="userName" class="col-sm-4 control-label">Name*
           </label>
            <div class="col-sm-7">
                <input type="text" name="name" data-parsley-trigger="change"
                    required placeholder="" class="form-control" id="name">
            </div>
        </div>

        <div class="form-group">
            <label for="userName" class="col-sm-4 control-label">Tag</label>
            <div class="col-sm-7">
                <input type="text" name="tag" data-parsley-trigger="change"
                    placeholder="" class="form-control" id="tag">
            </div>
        </div>
        <div class="form-group">
            <label for="userName" class="col-sm-4 control-label">Url</label>
            <div class="col-sm-7">
                <input type="url" required data-parsley-type="url" 
                      name="url"
                    data-parsley-trigger="change" required placeholder=""
                    class="form-control" id="url">

            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-4 col-sm-8">
                <button type="button" id="submit_video" 
                    class="btn btn-primary waves-effect waves-
         light">Submit</button>
                <button type="reset" onclick="Custombox.close();"
                    class="btn btn-default waves-effect waves-light m-l-
            5">Cancel</button>
            </div>
        </div>
    </form>
        </div>
        </div>
     <!-- ============================================================== -->
     <!-- End Right content here -->
   <!-- ============================================================== -->
     <script src="<?php echo BASE_PATH;?>assets/js/jquery-2.js"></script>
     <script type="text/javascript"
  src="<?php echo BASE_PATH;?>assets/js/jquery.validate.min.js"></script>
    <script
   src="<?php echo BASE_PATH;?>assets/parsleyjs/dist/parsley.min.js">
    </script>
      <script type="text/javascript">
       $(document).ready(function(){

    $('#submit_image').on('click', function() {
      $('#image_form').parsley().validate();
    });

        var image_list = $('#demo-foo-filtering').DataTable( {

     "processing": true,
     "serverSide": true,
     "ajax": {
         "url": "<?php echo BASE_URL;?>admin/ajax_image_list",
         "type": "POST"
     },
     "columns": [
         { "data": "id" },
         { "data": "name" },
         { "data": "tag" },
         { "data": "image" },        

     ],

    } );
     $('#image_form').parsley().on('form:success', function() {
     var formdata = $('#image_form').serialize();
            $.ajax({
                url:"<?php echo BASE_URL.'admin/submit_image';?>",
                type: 'POST',
                data: formdata,
                success: function(res) {
                        if(res==1){
                            Custombox.close();
                            $('#success_edit').css("display",'block');

             $('#success_edit').fadeIn().delay(3000).fadeOut();
                        }else{
                            //$('#error_edit').css("display",'block');
                        }

                }
            })
     });

    $('#submit_video').on('click', function() {
     $('#video_form').parsley().validate();
     //validateFront();
   });
    $('#video_form').parsley().on('form:success', function() {
     var formdata = $('#video_form').serialize();
            $.ajax({
                url:"<?php echo BASE_URL.'admin/submit_video';?>",
                type: 'POST',
                data: formdata,
                success: function(res) {
                        if(res==1){
                            Custombox.close();
                            $('#success_edit1').css("display",'block');

             $('#success_edit1').fadeIn().delay(3000).fadeOut();
                        }else{
                            //$('#error_edit').css("display",'block');
                        }
                }
            })
     });  
    $("#photo").change(function(){
        readURL(this);
    });
       });

 </script>