文件(图像)在codeigniter中上传问题

时间:2018-01-10 04:52:26

标签: php html codeigniter

实际上,我已经在前端显示后保存了数据库中的所有数据,在我身边的问题是我已经创建上传图像功能来保存数据库,我获取并显示前端,上传功能正在保存完整路径如:C:/xampp/www/htdocs/rentozy/admin/images/media/rajkumar-1515559187/1.jpg。所有图像保存文件夹也在前端只是这样:C:/xampp/www/htdocs/rentozy/admin/images/media/rajkumar-1515559187/1.jpg请我需要保存这样的数据库:(图片/media/rajkumar-1499778784/19510.jpg)请帮我解决这个问题,这是我第一次坐的是codeigniter请帮忙如何通过这个网址。

这是我的控制器代码:

function addNewMedia()
    {
        if($this->isAdmin() == TRUE)
        {
            $this->loadThis();
        }
        else
        {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('name','Name','trim|required|max_length[128]|xss_clean');
            $this->form_validation->set_rules('event_image','Pg Image');
           // $this->form_validation->set_rules('media_image','Image');
           // $this->form_validation->set_rules('date_added','Date','trim|required');

            if($this->form_validation->run() == FALSE)
            {
                $this->addNew();
            }
            else
            {
                $data = array(); $upload_data = array(); 
                $this->load->library('upload');

                $data['name'] = $this->input->post('name');
                $folder_srting = $data['name']."-".time();
                $data['name'] = $this->input->post('name');
               // print_r($folder_srting);
                $folder_string = str_replace(' ', '-', $folder_srting);// Replaces all spaces with hyphens.
                $folder_string = preg_replace('/[^A-Za-z0-9\-]/', '', $folder_srting);// Removes special chars.
                $folder_name   = preg_replace('/-+/', '-', strtolower($folder_string));// Replaces multiple hyphens with single one.
                print_r($folder_name);
                //$data['name'] = $this->input->post('name');
                //$pg_id = $this->input->post('pg_id');

                if ($_FILES['event_image']['error'] != 4)
                {

                    $folder = $this->checkdirectory($folder_name);
                    //print_r($folder_name);
                    $this->upload->initialize($this->set_upload_options($folder));

                    if ( ! $this->upload->do_upload('event_image'))
                    {
                            $error = array('error' => $this->upload->display_errors());
                            print_r($error); die;
                    }
                    else
                    {
                            $upload_data['banner_data'] = $this->upload->data();
                            //print_r($upload_data['banner_data']);die;
                            $upload_data['bannerfilepath'] = $upload_data['banner_data']['full_path'];
                            //print_r($upload_data['bannerfilepath']);die;


                    }
                    foreach($upload_data['banner_data'] as $bannerfilepath){
                            $data['banner_image_path'] = str_ireplace(FCPATH,"", $upload_data['banner_data']['full_path']);
                            //print_r($data['banner_image_path']);die;

                    } 
                   $event_image = $data['banner_image_path'];
                   //print_r($event_image);die;
                }
               // $name = ucwords(strtolower($this->input->post('name')));
                $event_image = $event_image;
                //print_r($event_image);die;
                $name = $this->input->post('name');                                
                $address = $this->input->post('pg_address');                                
                $incharge_name = $this->input->post('pg_incharge_name');
                $incharge_mobile = $this->input->post('pg_incharge_mobile');
                $email = $this->input->post('pg_email');
                $mediaInfo = array('name'=>$name,'event_image'=>$event_image,'pg_address'=>$address,'pg_incharge_name'=>$incharge_name,'pg_incharge_mobile'=> $incharge_mobile,'pg_email'=>$email,'folder_name'=>$folder);
                //echo "<pre>";print_r($mediaInfo);die;
                $this->load->model('media_model');
                //echo "<pre>";print_r($mediaInfo);die;
                $result = $this->media_model->addNewMedia($mediaInfo);
                if($result > 0)
                {
                    $this->session->set_flashdata('success', 'New Pg created successfully');
                }
                else
                {
                    $this->session->set_flashdata('error', 'Pg creation failed');
                }

                redirect('mediaListing');
            }
        }
    }
function editMedia()
    {
        if($this->isAdmin() == TRUE)
        {
            $this->loadThis();
        }
        else
        {
            $this->load->library('form_validation');
            $eventId = $this->input->post('pg_id');
            $this->form_validation->set_rules('name','Name','trim|required|max_length[128]|xss_clean');
            $this->form_validation->set_rules('event_image','Pg Image');
            //$this->form_validation->set_rules('event_description','Event Description','required|max_length[200]');
           // $this->form_validation->set_rules('start_date','Start Date','trim|required');
            //$this->form_validation->set_rules('end_date','End Date','trim|required');
            //$this->form_validation->set_rules('additional_images','Additional Images');
            //$this->form_validation->set_rules('short_description','Short Description','required');

            if($this->form_validation->run() == FALSE)
            {
                $this->editNew($eventId);
            }

            else
            {
                $data = array(); $upload_data = array(); 
                $this->load->library('upload');

                $existing_folder = $_POST['folder_name'];
                //print_r($existing_folder);die;

                if(isset($_POST['image_exists']) && $_POST['image_exists']!= '')
                $temp_attachment = $_POST['image_exists'];

                $folder = $this->checkdirectory($existing_folder);

                 if (isset($_FILES['event_image']['name']) && $_FILES['event_image']['error'][0] != 4 && $_FILES['event_image']['name']!='') {
                    $this->upload->initialize($this->set_upload_options($folder));

                if ( ! $this->upload->do_upload('event_image'))
                    {
                            $error = array('error' => $this->upload->display_errors());
                            //print_r($error); die;
                    }
                    else
                    {
                           $upload_data['banner_data'] = $this->upload->data();
                            $upload_data['bannerfilepath'] = $upload_data['banner_data']['full_path'];

                    }

                    // GET REQUIRED BANNER IMAGES FILE PATH FROM FULL PATH
                    foreach($upload_data['banner_data'] as $bannerfilepath){
                            $data['banner_image_path'] = str_ireplace(FCPATH,"", $upload_data['banner_data']['full_path']);
                             print_r($data['banner_image_path']);die;

                    }
                    $event_image = $data['banner_image_path'];
                    //print_r($event_image);die;
                  }
                  else{
                    // echo "sfgjdf";
                        $event_image = $temp_attachment;
                       // print_r($event_image);die;
                    }


                $event_image = $event_image;
                $name = $this->input->post('name');
                $pg_address = $this->input->post('pg_address');

                $pg_incharge_name = $this->input->post('pg_incharge_name');
                $pg_incharge_mobile = $this->input->post('pg_incharge_mobile');
                $pg_email = $this->input->post('pg_email');
               // $additional_images = $additional_images;
                $mediaInfo = array('name'=>$name,'event_image'=>$event_image,'pg_address'=>$pg_address,'pg_incharge_name'=>$pg_incharge_name,'pg_incharge_mobile'=>$pg_incharge_mobile,'pg_email'=>$pg_email,'folder_name'=>$folder);
                //echo "<pre>";print_r($mediaInfo);die;
                $result = $this->media_model->editMedia($mediaInfo, $eventId);
                if($result == true)
                {
                    $this->session->set_flashdata('success', 'Pg updated successfully');
                }
                else
                {
                    $this->session->set_flashdata('error', 'Pg updation failed');
                }

                redirect('mediaListing');
            }
        }
    }

这是我的模特:

 function addNewMedia($mediaInfo)
    {
        $this->db->trans_start();
        $this->db->insert('tbl_master_property', $mediaInfo);

        $insert_id = $this->db->insert_id();

        $this->db->trans_complete();

        return $insert_id;
    }

function getMediaInfo($eventId)
    {
        $this->db->select('pg_id, name,event_image,pg_address,pg_incharge_name,pg_incharge_mobile,pg_email,folder_name');
        $this->db->from('tbl_master_property');
        $this->db->where('status', 0);
        $this->db->where('pg_id', $eventId);
        $query = $this->db->get();

        return $query->result();
    }
function editMedia($mediaInfo, $eventId)
    {
        $this->db->where('pg_id', $eventId);
        $this->db->update('tbl_master_property', $mediaInfo);
        return TRUE;
    }

这里我的视图文件代码:

<?php
define("IMAGE_PATH", "http://localhost/rentozy/admin/");

$eventId = '';
$name = '';
$pg_address = '';
$pg_incharge_name = '';
$pg_incharge_mobile = '';
$pg_email ='';
$event_image = '';
$folder_name = '';




if(!empty($mediaInfo))
{
    foreach ($mediaInfo as $ef)
    {
        $eventId = $ef->pg_id;
        $name = $ef->name;
        $pg_address = $ef->pg_address;
        $pg_incharge_name = $ef->pg_incharge_name;
        $pg_incharge_mobile = $ef->pg_incharge_mobile;
        $pg_email = $ef->pg_email;
        $event_image = $ef->event_image;
        $folder_name = $ef->folder_name;


    }
}


?>
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">
//<![CDATA[
        bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
  //]]>
  </script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <i class="fa fa-users"></i> Property Management
        <small>Add / Edit Property</small>
      </h1>
    </section>

    <section class="content">

        <div class="row">
            <!-- left column -->
            <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12">
              <!-- general form elements -->



                <div class="box box-primary">
                    <div class="box-header">
                        <h3 class="box-title">Enter Property Details</h3>
                    </div><!-- /.box-header -->
                    <!-- form start -->

                    <form role="form" action="<?php echo base_url() ?>editMedia" method="post" id="editEvent" role="form" enctype="multipart/form-data" files="true">
                        <div class="box-body">
                            <div class="row">
                                <div class="col-md-6">                                
                                    <div class="form-group">
                                        <label for="event_name">Name</label>
                                        <input type="text" class="form-control" id="name" placeholder="Name" name="name" value="<?php echo $name; ?>" maxlength="128" readonly>
                                           <input type="hidden" value="<?php echo $eventId; ?>" name="pg_id" id="eventId" />
                                        <input type="hidden" value="<?php echo $folder_name; ?>" name="folder_name"/>   
                                    </div>
                                </div>

                            </div>
                            <div class="row">           
                                <div class="col-md-6" style="padding-bottom:15px;">
                                    <div class="form-group">
                                        <label for="description" class="pull-left">Pg Address</label>
                                        <textarea rows="6" cols="50" name="pg_address" class="pull-left" style="width:100%;" value="<?php echo $pg_address;?>" id="pgaddress"><?php echo $pg_address;?></textarea>
                                    </div>
                                </div>
                                <div class="col-md-6" style="padding-bottom:15px;">
                                    <div class="form-group">
                                        <label for="description" class="pull-left">Pg Incharge Name</label>
                                        <div class="clearfix"></div>
                                        <textarea rows="6" cols="50" name="pg_incharge_name" class="pull-left" style="width:100%;" value="<?php echo $pg_incharge_name;?>" id="pg_incharge_name" ><?php echo $pg_incharge_name;?></textarea>
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="start-date">Pg Incharge Mobile</label>
                                        <input type="text" class="form-control required pg_incharge_mobile" value="<?php echo $pg_incharge_mobile;?>" id="pg_incharge_mobile" name="pg_incharge_mobile">
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label for="end-date">Pg Email</label>
                                        <input type="text" class="form-control pg_email" value="<?php echo $pg_email;?>" id="pg_email" name="pg_email">
                                    </div>
                                    <div class="col-md-6">
                                    <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="event_image">Pg Image</label>
                                        <input type="file" value="<?php echo $event_image; ?>" class="form-control file_change1" id="eventimage"  name="event_image">
                                        <img src="<?php echo IMAGE_PATH.$event_image;?>" width="100px" height="50px">
                                        <input type="hidden" name="image_exists" value="<?php echo $event_image;?>" class="form-control" id="eventimage" placeholder="Enter Image Text" aria-describedby="fileHelp">
                                        <div><?php echo $event_image;?></div>

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


                                </div>

                            </div>
                        </div>

1 个答案:

答案 0 :(得分:0)

我不知道你是否考虑过它,但是你可以轻松地使用PHP的str_replace来做你需要的。

<?php

$path = $upload_data['banner_data']['full_path'];

$dir = 'C:/xampp/www/htdocs/rentozy/admin/';

$url = str_replace( $dir, '', $path );

echo $url;

在CodeIgniter中,如果要保存到文档根目录之外的路径,则可以使用FCPATH常量来简化此操作。因此,如果您上传文件夹的路径位于名为/uploads/的目录中,而/uploads/位于文档根目录,那么:

<?php

$path = $upload_data['banner_data']['full_path'];

$dir = FCPATH . 'uploads/';

$url = str_replace( $dir, '', $path );

echo $url;

这只是一个例子,但很容易