0

Actually, I have saved all the data in database after i have show in front end,in my side issue is i have created upload image function to save database after i fetch and display the front end,upload function is taking to save full path like :C:/xampp/www/htdocs/rentozy/admin/images/media/rajkumar-1515559187/1.jpg. all the images saved folder also but in front end is coming like this only : C:/xampp/www/htdocs/rentozy/admin/images/media/rajkumar-1515559187/1.jpg please i need save database like this : (images/media/rajkumar-1499778784/19510.jpg) please help me how will resolve this this is my first sit is codeigniter please help how will pass like this url.

Here my code for controller:

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');
            }
        }
    }

here my model:

 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;
    }

here my view file code:

<?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>
7
  • image is successfully upload on folder ? Commented Jan 10, 2018 at 5:00
  • yaa it is uploaded successfully but it is taking full path for database and fetch fron end meanse it showing path only not show image, i need save like this :images/media/bollywoodwesterndanceworkshop-1500442625/Penguins.jpg how please help Commented Jan 10, 2018 at 5:03
  • Please try that way : $abc = ['banner_data']['name']; $path = 'images/media/bollywoodwesterndanceworkshop-1500442625/' . $abc; Commented Jan 10, 2018 at 5:14
  • it showing issue like : Message: Undefined index: name Commented Jan 10, 2018 at 5:24
  • 1
    that can't be, just your FCPATH nothing else. also please answer my question so i can help. Commented Jan 10, 2018 at 6:25

1 Answer 1

0

I don't know if you've thought about it, but you could easily use PHP's str_replace to do what you need.

<?php

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

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

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

echo $url;

In CodeIgniter, if you are saving to a path that is a directory off of document root, you can use the FCPATH constant to make this easy. So if your path to your upload folder is in a directory named /uploads/, and /uploads/ is at document root, then:

<?php

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

$dir = FCPATH . 'uploads/';

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

echo $url;

This is just an example, but it is easy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.