การลบไฟล์ และการใช้ path helper ร่วมในการตรวจสอบไฟล์

เขียนเมื่อ 8 ปีก่อน โดย Ninenik Narkdee
codeigniter 3 codeigniter path

คำสั่ง การ กำหนด รูปแบบ ตัวอย่าง เทคนิค ลูกเล่น การประยุกต์ การใช้งาน เกี่ยวกับ codeigniter 3 codeigniter path

ดูแล้ว 6,875 ครั้ง




การลบไฟล์ในที่นี้จะขออ้างอิงจากตอนที่แล้ว เป็นแนวทาง
 
การอัพโหลดไฟล์ด้วย file uploading class ใน codeigniter 
 
โดยเราจะดูในส่วนของการแก้ไขข้อมูล คือ ตามรูปแบบการแก้ไขข้อมูล
หากเมื่อเรามีการอัพโหลดไฟล์รูปภาพเข้าไป จากการใช้งาน file uploading
class ของตอนที่แล้ว เมื่อมีการแก้ไขและอัพโหลดไฟล์ไปใหม่ เราไม่ได้ทำการลบ
ไฟล์เดิมทิ้ง และ กรณีเช่น เราไม่ต้องการใช้รูปภาพ คือต้องการให้มีแค่หัวข้อ กับ
รายละเอียด ดังนั้นจึงควรที่จะสามารถ เลือกที่จะลบไฟล์ที่อัพโหลดไปแล้วได้
เหตุผลทั้งสองกรณีจึงมาสู่เนื้อหาของตอนนี้
 
 

การใช้ path helper ช่วยในการระบุตำแหน่งของไฟล์ที่แท้จริง

 
สำหรับ path helper ที่จะเราจะนำมาใช้งานร่วมนี้ จะมีฟังก์ชั่นแค่ฟังก์ชั่นเดียว
เพื่อใช้ในการระบุตำแหน่งของไฟล์ที่เราต้องการตรวจสอบ
ก่อนอื่นให้เราโหลด path helper ก่อนดังนี้
 
        $this->load->helper('path');
 
โดยให้กำหนดในไฟล์ Service_model.php ในโฟลเดอร์ apps > medels > admin
ในส่วนของฟังก์ชั่น __construct()  จะได้เป็น 
 
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="bg-danger" style="padding:3px 10px;">', '</div>');
        $this->load->library('upload');
        $this->load->helper('path');
    }
 
จากนั้นมาที่ไฟล์ views ไฟล์ admin_service.php ในส่วน $action เท่ากับ edit
สิ่งที่เราจะทำคือ
1. สร้างตัวแปรกำหนด relative path ไฟล์
 
$fileCheck = './upload/'.$row['service_img'];   
 
2. ใช้ฟังก์ชั่น set_realpath() ที่ได้จากการโหลด path helper สร้างตำแหน่งไฟล์ที่แท้จริง
 
$full_fileCheck = set_realpath($fileCheck);
 
ตัวอย่างเช่น 
 
$fileCheck = './upload/mypicture4.gif';   
$full_fileCheck = set_realpath($fileCheck);
// จะได้ค่าตัวแปร $full_fileCheck เป็น C:xampphtdocslearnciuploadmypicture4.gif
 
3. ตรวจสอบว่ามีไฟล์นี้จริงหรือไม่ และเป็นประเภทไฟล์หรือไม่(ไม่ใช่โฟลเดอร์)
 
if(file_exists($full_fileCheck) && is_file($full_fileCheck)){ 

}
 
4. ถ้าเป็นไฟล์จริงให้แสดงรูปภาพเดิมขนาดเล็กๆ ไว้ตรวจทานความถูกต้องการอัพโหลด
 
<img src="<?=base_url('upload/'.$row['service_img'])?>" style="width:150px;">
 
5. จากนั้นให้เพิ่ม input checkbox ให้สามารถเลือกได้ว่า จะลบไฟล์นี้หรือไม่ โดยให้มีค่าเท่ากับ
ตำแหน่งจริงของไฟล์ที่ต้องการลบจากตัวแปร $full_fileCheck และในที่นี้ใช้ชื่อว่า d_service_image
 
<input type="checkbox" name="d_service_image" value="<?=$full_fileCheck?>"> คลิกเลือกหากต้องการลบไฟล์
 
ทั้งหมดใน 5 ขั้นตอน เราจะได้ในส่วนที่ต้องการดังนี้
 
        <input type="file" name="service_image" >
        <input type="hidden" name="h_service_image" value="<?=$row['service_img']?>">       
<?php
$fileCheck = './upload/'.$row['service_img'];   
$full_fileCheck = set_realpath($fileCheck);
if(file_exists($full_fileCheck) && is_file($full_fileCheck)){ 
?>        
        <img src="<?=base_url('upload/'.$row['service_img'])?>" style="width:150px;">
        <br>
        <input type="checkbox" name="d_service_image" value="<?=$full_fileCheck?>"> คลิกเลือกหากต้องการลบไฟล์
        <br>
<?php } ?>    
 

โค้ดทั่้งหมดในส่วนของ $action เท่ากับ edit

 
<?php if($action=="edit"){?>
<a href="<?=base_url('admin/service')?>" class="btn btn-warning btn-sm">< Back</a>
<br><br>
<?php
$this->form_validation->set_rules('service_title', 'Title', 'required');
$this->form_validation->set_rules('service_detail', 'Detail', 'required');

if($this->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน
//    echo "Error";
}else{  // กรณีตรวจสอบผ่าน
    $query = $this->service_model->edit($id);
    if($query){ // เมื่อแก้ไขข้อมูลเรียบร้อยแล้ว
        redirect('admin/service'); // ไปหน้า service  
    }
}
                            
if(validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้
    echo   validation_errors();
    echo "<br>";
}
if($this->upload->display_errors()){
    echo $this->upload->display_errors('<div class="bg-danger" style="padding:3px 10px;">', '</div>');
    echo "<br>";    
}
// เรียกใช้ฟังก์ชั่น view() ดึงข้อมูลมาแสดงก่อนแก้ไข    
$row = $this->service_model->view($id);             
?>
<form action="<?=base_url('admin/service/edit/'.$id)?>" method="post" enctype="multipart/form-data">
<table class="table table-bordered">
<thead>
    <tr class="active">
        <th colspan="2">Edit Service</th>
    </tr>
</thead>
<tbody>
    <tr >
        <th width="120">Title:</th>
        <td>
            <input type="text" name="service_title" value="<?=$row['service_title']?>" style="width:500px;">
        </td>
    </tr>
    <tr>
        <th width="120">Detail:</th>
        <td>
        <textarea name="service_detail" cols="85" rows="10"><?=$row['service_detail']?></textarea>
        </td>
    </tr>    
    <tr>
        <th width="120">Images:</th>
        <td>
        <input type="file" name="service_image" >
        <input type="hidden" name="h_service_image" value="<?=$row['service_img']?>">       
<?php
$fileCheck = './upload/'.$row['service_img'];   
$full_fileCheck = set_realpath($fileCheck);
if(file_exists($full_fileCheck) && is_file($full_fileCheck)){ 
?>        
        <img src="<?=base_url('upload/'.$row['service_img'])?>" style="width:150px;">
        <br>
        <input type="checkbox" name="d_service_image" value="<?=$full_fileCheck?>"> คลิกเลือกหากต้องการลบไฟล์
        <br>
<?php } ?>        
        </td>
    </tr>    
    <tr>
        <th></th>
        <td>
            <input type="submit" class="btn btn-success btn-sm" name="btn_add" value="Edit Service">
        </td>
    </tr>
</tbody>
</table>   

        
</form>
<?php } ?>
 
 

ต่อไปเรากลับมาดูในส่วนของ models ไฟล์ Service_model.php ในโฟลเดอร์ apps > models >admin

ในส่วนของฟังก์ชั่นการแก้ไข edit()
โค้ดเดิม
 
    public function edit($id){
        $config['upload_path'] = './upload/';  // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค
        $config['allowed_types'] = 'gif|jpg|png'; // ปรเเภทไฟล์ 
        $config['max_size']     = '0';  // ขนาดไฟล์ (kb)  0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB
        $config['max_width'] = '1024';  // ความกว้างรูปไม่เกิน
        $config['max_height'] = '768'; // ความสูงรูปไม่เกิน
        $config['file_name'] = 'mypicture';  // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม

        $this->upload->initialize($config);    // เรียกใช้การตั้งค่า  
        $this->upload->do_upload('service_image'); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image
        
        $file_upload=$this->input->post('h_service_image');  // เก็บชื่อไฟล์เพิมถ้ามี
        if(!$this->upload->display_errors()){ // ถ้าไม่มี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล
            $file_upload=$this->upload->data('file_name');  // เก็บชื่อไฟล์ใหม่           
        }else{
            // ถ้า error ในกรณีเลือกไฟล์แล้วไม่ผ่าน
            if($this->upload->data('file_type')){ // เช่น ประเภทไม่ถูกต้อง
                return; // ต้อง return เพื่อให้แสดง error
            }
        }        
        $newdata = array(
            'service_title' => $this->input->post('service_title'),
            'service_detail' => $this->input->post('service_detail'),
            'service_img' => $file_upload,
            'service_update' => date("Y-m-d H:i:s")
        );
        return $this->db->update('tbl_service', $newdata,array('service_id'=>$id));
    }  
 
 

เราจะปรับแก้ไขใหม่เล็กน้อยตามเงื่อนการทำงานดังนี้

1. ก่อนที่จะทำการอัพโหลดไฟล์ใหม่ ถ้ามีการเลือกอัพโหลด ให้ตรวจสอบว่ามีการส่งคำขอ
ว่าต้องการลบไฟล์เดิมหรือไม่ โดยตรวจสอบจาก input checkbox ที่ชื่อ d_service_image
ที่มีการส่งตำแหน่งไฟล์ทีแท้จริงมา
 
$fileExist=$this->input->post('d_service_image');
 
2. ตรวจสอบก่อนลบไฟล์ เมื่อมีการส่งไฟล์ที่ต้องการลบมา ให้ตรวจสอบว่า มีไฟล์นี้จริงหรือไม่
และเป็นประเภทไฟล์หรือไม่(ไม่ใช่โหลเดอร์) โดยถ้าเป็นจริงก็ให้ทำการลบไฟล์นั้นด้วยคำสั่ง
unlink() และกำหนดชื่อไฟล์ใหม่เป็นค่าว่างไปก่อน
 
        if(file_exists($fileExist) && is_file($fileExist)){
            unlink($fileExist);  
            $file_upload="";
        }else{

        }
 
3. ถ้าไม่มีไฟล์นั้นอยู่จริง หรือไม่ได้มีการส่งคำสั่งลบไฟล์เข้ามา ก็ให้ชื่อไฟล์ มีค่าเท่ากับชื่อไฟล์เดิม(ถ้ามี)
โดยได้ถูกส่งมาด้วย input hidden ที่ชื่อ h_service_image
 
$file_upload=$this->input->post('h_service_image');  // เก็บชื่อไฟล์เพิมถ้ามี
 
4. มีบางกรณีที่มีชื่อไฟล์ในฐานข้อมูล ทำให้ตัวแปร hidden ส่งค่าชื่อไฟล์มา แต่ความเป็น
ในระบบไม่มีชื่อไฟล์นี้ เราจึงเพิ่มในส่วนของการตรวจสอบไฟล์อีกครั้งว่า ชื่อไฟล์เดิมที่ส่งมา
พอตรวจสอบจากตำแหน่งจริงๆ แล้วมีไฟล์อยู่จริงหรือไม่ ถ้าไม่มี เราก็จะให้ชื่อไฟล์เป็นค่าว่าง
 
        if(file_exists($fileExist) && is_file($fileExist)){
            unlink($fileExist);  
            $file_upload="";
        }else{
            $file_upload=$this->input->post('h_service_image');  // เก็บชื่อไฟล์เพิมถ้ามี
            $fileCheck = './upload/'.$file_upload;   
            $full_fileCheck = set_realpath($fileCheck);
            if(!file_exists($full_fileCheck) || !is_file($full_fileCheck)){
                $file_upload="";
            }
        }
 
5. จากนั้นก็ให้ทำการอัพโหลดไฟล์ปกติถ้ามีการเลือกไฟล์เพื่ออัพโหลด
 
        $this->upload->do_upload('service_image'); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image   


 
ดูโค้ดเต็มในส่วนของฟังก์ชั่น edit() จะได้ดังนี้
 
    public function edit($id){
        $config['upload_path'] = './upload/';  // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค
        $config['allowed_types'] = 'gif|jpg|png'; // ปรเเภทไฟล์ 
        $config['max_size']     = '0';  // ขนาดไฟล์ (kb)  0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB
        $config['max_width'] = '1024';  // ความกว้างรูปไม่เกิน
        $config['max_height'] = '768'; // ความสูงรูปไม่เกิน
        $config['file_name'] = 'mypicture';  // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม

        $this->upload->initialize($config);    // เรียกใช้การตั้งค่า  
        
        $fileExist=$this->input->post('d_service_image');
        if(file_exists($fileExist) && is_file($fileExist)){
            unlink($fileExist);  
            $file_upload="";
        }else{
            $file_upload=$this->input->post('h_service_image');  // เก็บชื่อไฟล์เพิมถ้ามี
            $fileCheck = './upload/'.$file_upload;   
            $full_fileCheck = set_realpath($fileCheck);
            if(!file_exists($full_fileCheck) || !is_file($full_fileCheck)){
                $file_upload="";
            }
        }
                
        $this->upload->do_upload('service_image'); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image        
        if(!$this->upload->display_errors()){ // ถ้าไม่มี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล
            $file_upload=$this->upload->data('file_name');  // เก็บชื่อไฟล์ใหม่           
        }else{
            // ถ้า error ในกรณีเลือกไฟล์แล้วไม่ผ่าน
            if($this->upload->data('file_type')){ // เช่น ประเภทไม่ถูกต้อง
                return; // ต้อง return เพื่อให้แสดง error
            }
        }        
        $newdata = array(
            'service_title' => $this->input->post('service_title'),
            'service_detail' => $this->input->post('service_detail'),
            'service_img' => $file_upload,
            'service_update' => date("Y-m-d H:i:s")
        );
        return $this->db->update('tbl_service', $newdata,array('service_id'=>$id));
    }  
 
 

เราจะได้ไฟล์ Service_model.php ในโฟลเดอร์ apps > models > admin ดังนี้

 
<?php
class Service_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="bg-danger" style="padding:3px 10px;">', '</div>');
        $this->load->library('upload');
        $this->load->helper('path');
    }
    
    public function getlist(){
        $query = $this->db->get('tbl_service');
        return $query->result_array();
    }    
    
    public function create(){
        $config['upload_path'] = './upload/';  // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค
        $config['allowed_types'] = 'gif|jpg|png'; // ปรเเภทไฟล์ 
        $config['max_size']     = '0';  // ขนาดไฟล์ (kb)  0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB
        $config['max_width'] = '1024';  // ความกว้างรูปไม่เกิน
        $config['max_height'] = '768'; // ความสูงรูปไม่เกิน
        $config['file_name'] = 'mypicture';  // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม

        $this->upload->initialize($config);    // เรียกใช้การตั้งค่า  
        $this->upload->do_upload('service_image'); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image
        
        $file_upload="";  // กำหนดชื่อไฟล์เป็นค่าว่าง 
        if(!$this->upload->display_errors()){ // ถ้าไม่มี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล
            $file_upload=$this->upload->data('file_name');
        }
        $newdata = array(
            'service_id' => NULL,
            'service_title' => $this->input->post('service_title'),
            'service_detail' => $this->input->post('service_detail'),
            'service_img' => $file_upload,
            'service_update' => date("Y-m-d H:i:s")
        );
        return $this->db->insert('tbl_service', $newdata);                
    }
    
    public function view($id){  // มี $id เป็น parameter ไว้กำหนดเงื่อนไข
        $query = $this->db->get_where('tbl_service',array('service_id'=>$id));
        return $query->row_array(); // ส่งข้อมูลผลัพธ์กลับเป็น array แถวข้อมูล
    }    
    
    public function edit($id){
        $config['upload_path'] = './upload/';  // โฟลเดอร์ ตำแหน่งเดียวกับ root ของโปรเจ็ค
        $config['allowed_types'] = 'gif|jpg|png'; // ปรเเภทไฟล์ 
        $config['max_size']     = '0';  // ขนาดไฟล์ (kb)  0 คือไม่จำกัด ขึ้นกับกำหนดใน php.ini ปกติไม่เกิน 2MB
        $config['max_width'] = '1024';  // ความกว้างรูปไม่เกิน
        $config['max_height'] = '768'; // ความสูงรูปไม่เกิน
        $config['file_name'] = 'mypicture';  // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเพิม

        $this->upload->initialize($config);    // เรียกใช้การตั้งค่า  
        
        $fileExist=$this->input->post('d_service_image');
        if(file_exists($fileExist) && is_file($fileExist)){
            unlink($fileExist);  
            $file_upload="";
        }else{
            $file_upload=$this->input->post('h_service_image');  // เก็บชื่อไฟล์เพิมถ้ามี
            $fileCheck = './upload/'.$file_upload;   
            $full_fileCheck = set_realpath($fileCheck);
            if(!file_exists($full_fileCheck) || !is_file($full_fileCheck)){
                $file_upload="";
            }
        }
                
        $this->upload->do_upload('service_image'); // ทำการอัพโหลดไฟล์จาก input file ชื่อ service_image        
        if(!$this->upload->display_errors()){ // ถ้าไม่มี error อัพไฟล์ได้ ให้เอาใช้ไฟล์ใส่ตัวแปร ไว้บันทึกลงฐานข้อมูล
            $file_upload=$this->upload->data('file_name');  // เก็บชื่อไฟล์ใหม่           
        }else{
            // ถ้า error ในกรณีเลือกไฟล์แล้วไม่ผ่าน
            if($this->upload->data('file_type')){ // เช่น ประเภทไม่ถูกต้อง
                return; // ต้อง return เพื่อให้แสดง error
            }
        }        
        $newdata = array(
            'service_title' => $this->input->post('service_title'),
            'service_detail' => $this->input->post('service_detail'),
            'service_img' => $file_upload,
            'service_update' => date("Y-m-d H:i:s")
        );
        return $this->db->update('tbl_service', $newdata,array('service_id'=>$id));
    }  
    
    public function delete($id){
        return $this->db->delete('tbl_service', array('service_id' =>$id)); 
        // คืนค่าผลการคิวรี่
    }        

}
 
 

และไฟล์ admin_service.php ในโฟลเดอร์ apps > views > admin ดังนี้

 
<div class="container">
    
Service  
<br><br>
<?php if($action==null){?>
<?php
$result = $this->service_model->getlist();
?>
<a href="<?=base_url('admin/service/create')?>" class="btn btn-primary btn-sm">Create</a>
<br><br>
<table class="table table-striped table-bordered table-condensed">
    <thead>
        <tr>
            <th width="50" class="text-center">#</th>
            <th>Title</th>
            <th width="150" class="text-center">Modify Date</th>
            <th width="150" class="text-center">Manage</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i_num=0;
        if(count($result)>0){
            foreach($result as $row){
                $i_num++;
        ?>
        <tr>
            <td class="text-center"><?=$i_num?></td>
            <td><?=$row['service_title']?></td>
            <td class="text-center"><?=$row['service_update']?></td>
            <td class="text-center">
                <a href="<?=base_url('admin/service/edit/'.$row['service_id'])?>" class="btn btn-success btn-sm">
                <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
                </a>
                &nbsp;&nbsp;
                 <a href="<?=base_url('admin/service/delete/'.$row['service_id'])?>" class="btn btn-danger btn-sm">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                </a>
                
            </td>
        </tr>
        <?php } ?>
        <?php } ?>
    </tbody>
</table>
<?php } ?>

<?php if($action=="create"){?>
<a href="<?=base_url('admin/service')?>" class="btn btn-warning btn-sm">< Back</a>
<br><br>
<?php
$this->form_validation->set_rules('service_title', 'Title', 'required');
$this->form_validation->set_rules('service_detail', 'Detail', 'required');

if($this->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน
//    echo "Error";
}else{  // กรณีตรวจสอบผ่าน
    $query = $this->service_model->create();
    if($query){ // เมื่อเพิ่มข้อมูลเรียบร้อยแล้ว
        redirect('admin/service'); // ไปหน้า service  
    }
}
                            
if(validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้
    echo   validation_errors();
    echo "<br>";
}                           
if($this->upload->display_errors()){
    echo $this->upload->display_errors('<div class="bg-danger" style="padding:3px 10px;">', '</div>');
    echo "<br>";    
}
?>
<form action="<?=base_url('admin/service/create')?>" method="post" enctype="multipart/form-data">
<table class="table table-bordered">
<thead>
    <tr class="active">
        <th colspan="2">Add New Service</th>
    </tr>
</thead>
<tbody>
    <tr >
        <th width="120">Title:</th>
        <td>
            <input type="text" name="service_title" value="<?=set_value('service_title')?>" style="width:500px;">
        </td>
    </tr>
    <tr>
        <th width="120">Detail:</th>
        <td>
        <textarea name="service_detail" cols="85" rows="10"><?=set_value('service_detail')?></textarea>
        </td>
    </tr>    
    <tr>
        <th width="120">Images:</th>
        <td>
        <input type="file" name="service_image" >
        </td>
    </tr>    
    <tr>
        <th></th>
        <td>
            <input type="submit" class="btn btn-success btn-sm" name="btn_add" value="Add Service">
        </td>
    </tr>
</tbody>
</table>    
    

</form>
<?php } ?>


<?php if($action=="edit"){?>
<a href="<?=base_url('admin/service')?>" class="btn btn-warning btn-sm">< Back</a>
<br><br>
<?php
$this->form_validation->set_rules('service_title', 'Title', 'required');
$this->form_validation->set_rules('service_detail', 'Detail', 'required');

if($this->form_validation->run() === FALSE){ // ถ้าตรวจสอบไม่ผ่าน ให้ทำงาน
//    echo "Error";
}else{  // กรณีตรวจสอบผ่าน
    $query = $this->service_model->edit($id);
    if($query){ // เมื่อแก้ไขข้อมูลเรียบร้อยแล้ว
        redirect('admin/service'); // ไปหน้า service  
    }
}
                            
if(validation_errors()){ // ถ้ามีเงื่อนไขหนึ่งใดไม่ผ่าน ให้แสดง ข้อความ error ตำแหน่งนี้
    echo   validation_errors();
    echo "<br>";
}
if($this->upload->display_errors()){
    echo $this->upload->display_errors('<div class="bg-danger" style="padding:3px 10px;">', '</div>');
    echo "<br>";    
}
// เรียกใช้ฟังก์ชั่น view() ดึงข้อมูลมาแสดงก่อนแก้ไข    
$row = $this->service_model->view($id);             
?>
<form action="<?=base_url('admin/service/edit/'.$id)?>" method="post" enctype="multipart/form-data">
<table class="table table-bordered">
<thead>
    <tr class="active">
        <th colspan="2">Edit Service</th>
    </tr>
</thead>
<tbody>
    <tr >
        <th width="120">Title:</th>
        <td>
            <input type="text" name="service_title" value="<?=$row['service_title']?>" style="width:500px;">
        </td>
    </tr>
    <tr>
        <th width="120">Detail:</th>
        <td>
        <textarea name="service_detail" cols="85" rows="10"><?=$row['service_detail']?></textarea>
        </td>
    </tr>    
    <tr>
        <th width="120">Images:</th>
        <td>
        <input type="file" name="service_image" >
        <input type="hidden" name="h_service_image" value="<?=$row['service_img']?>">       
<?php
$fileCheck = './upload/'.$row['service_img'];   
$full_fileCheck = set_realpath($fileCheck);
if(file_exists($full_fileCheck) && is_file($full_fileCheck)){ 
?>        
        <img src="<?=base_url('upload/'.$row['service_img'])?>" style="width:150px;">
        <br>
        <input type="checkbox" name="d_service_image" value="<?=$full_fileCheck?>"> คลิกเลือกหากต้องการลบไฟล์
        <br>
<?php } ?>        
        </td>
    </tr>    
    <tr>
        <th></th>
        <td>
            <input type="submit" class="btn btn-success btn-sm" name="btn_add" value="Edit Service">
        </td>
    </tr>
</tbody>
</table>   

        
</form>
<?php } ?>

<?php if($action=="delete"){?>
<?php
$query = $this->service_model->delete($id);                                  
?>
<a href="<?=base_url('admin/service')?>" class="btn btn-warning btn-sm">< Back</a>
<br><br>
<?php if($query){?>
<div class="bg-success text-center" style="padding:10px;">
    <p class="text-success">Delete data complete</p>
    <a href="<?=base_url('admin/service')?>" class="text-success">< Back > </a>
</div>
<?php } ?>
<?php } ?>

</div>
 
 
เป็นอันเสร็จสำหรับในการใช้งานการแก้ไข หรือลบรูปภาพ รวมถึงการใช้งาน path helper
และก็ขอจบในส่วนของการใช้งานเกี่ยวกับระบบ admin service สำหรับแสดงข้อมูลในหน้า บริการของเราไว้เท่านี้
ตอนต่อไปเราจะมาดูในเรื่องของการใช้งาน cookie 


 
 


   เพิ่มเติมเนื้อหา ครั้งที่ 1 วันที่ 06-03-2020


ปรับความถูกต้องการอัพโหลด และการลบไฟล์รูป

 
การกำหนดชื่อ โดยการตั้งค่า 
 
  $config['file_name'] = 'mypicture';  // ชื่อไฟล์ ถ้าไม่กำหนดจะเป็นตามชื่อเดิม
 
จะเป็นการฟิกชื่อตามค่าที่เรากำหนด ดังนั้น กรณีเราอัพโหลดหลายๆ ข้อมูล หากชื่อซ้ำกัน จะเป็นการทับไฟล์เดิม
ให้ comment ปิดส่วนนี้ไป หรือให้เป็นค่าว่าง เพื่อให้ชื่อของไฟล์ที่อัพโหลดแทน 
 
  $config['file_name'] = ''; 
 
หรือสร้างรูปแบบชื่อไฟล์ไม่ให้ซ้ำกันแทนก็ได้เช่น
ใช้เวลา timestamp แทน
 
  $config['file_name'] = time(); // จะได้เป็น  1583489471
 
หรือใช้วันเดือนปีเวลา เป็น
 
  $config['file_name'] = date("YmdHis"); // จะได้เป็น 20200306110955
 
 
ในส่วนของการลบไฟล์จากปุ่ม ลบ
 
ตัวอย่างเดิม ไม่มีการลบไฟล์หากมี ดังนั้น เราต้องทำการดึงข้อมูลชื่อของไฟล์รูปภาพ แล้วทำการลบรูปออกก่อน
จากนั้นก็ทำการลบรายการปกติ ปรับโค้ดส่วนของฟังก์ชั่นการลบเป็นดังนี้
 
    public function delete($id){
        $query = $this->db->get_where('tbl_service',array('service_id'=>$id));
		if($query){
			$row = $query->row_array(); 
			$fileExist = $row['service_img'];
            $fileCheck = './upload/'.$fileExist;   
            $full_fileCheck = set_realpath($fileCheck);			
			if(file_exists($full_fileCheck) && is_file($full_fileCheck)){
				unlink($full_fileCheck);  
			}
		}		
        return $this->db->delete('tbl_service', array('service_id' =>$id)); 
        // คืนค่าผลการคิวรี่
    }        


กด Like หรือ Share เป็นกำลังใจ ให้มีบทความใหม่ๆ เรื่อยๆ น่ะครับ



อ่านต่อที่บทความ













URL สำหรับอ้างอิง





คำแนะนำ และการใช้งาน

สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก


  • ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
  • เปลี่ยน


    ( หรือ เข้าใช้งานผ่าน Social Login )







เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ