如何从Codeigniter中的文件夹中删除图像

时间:2016-07-18 06:45:57

标签: php codeigniter gallery

今天我大学的老师给我一个关于CodeIgniter的任务,他给了我这个控制器

Sub HighlightListed() 
Dim strConcatList As String 
Dim cell As Range 
For Each cell In Sheets("List").Range("A1:A30") 
       strConcatList = strConcatList & cell.Value & "|" 
Next cell 

For Each cell In Intersect(Sheets("Data").Range("A:A"), Sheets("Data").UsedRange) 'I assume my problem is here somewhere, its only highlights exact results. 
'If i am looking for "ABC123" it should also highlight cell like "PQRABC123" or ""XYZ_ABC123" 
    If InStr(strConcatList, cell.Value) > 0 Then 
          cell.Interior.Color = RGB(255, 0, 0) 
    End If 
Next cell 
End Sub

问题是我们必须创建一个处理函数删除的模型,所以它也可以删除文件夹中的照片,这里是模型

public function __construct()
{
    parent::__construct();
    $this->load->model('Gallery_model');
    $this->load->helper(['url','html','form']);
    $this->load->database();
    $this->load->library(['form_validation','session','image_lib']);
}

public function index()
{
    $data = ['images' => $this->Gallery_model->all()];
    $this->load->view($this->layoutgaleri, $data);
}

public function add(){
    $rules =    [
                    [
                            'field' => 'caption',
                            'label' => 'Caption',
                            'rules' => 'required'
                    ],
                    [
                            'field' => 'description',
                            'label' => 'Description',
                            'rules' => 'required'
                    ]
                ];

    $this->form_validation->set_rules($rules);

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('admin/layoutgaleriadd');
    }
    else
    {

        /* Start Uploading File */
        $config =   [
                        'upload_path'   => './asset/images/',
                        'allowed_types' => 'gif|jpg|png',
                        'max_size'      => 2000,
                        'max_width'     => 640,
                        'max_height'    => 480
                    ];

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('admin/layoutgaleriadd', $error);
        }
        else
        {
                $file = $this->upload->data();
                //print_r($file);
                $data = [
                            'file'          => 'asset/images/' . $file['file_name'],
                            'caption'       => set_value('caption'),
                            'description'   => set_value('description')
                        ];
                $this->Gallery_model->create($data);
                $this->session->set_flashdata('message','Gambar sudah ditambahkan..');
                redirect('admin/Galeri');
        }
    }
}

public function edit($id){
    $rules =    [
                    [
                            'field' => 'caption',
                            'label' => 'Caption',
                            'rules' => 'required'
                    ],
                    [
                            'field' => 'description',
                            'label' => 'Description',
                            'rules' => 'required'
                    ]
                ];

    $this->form_validation->set_rules($rules);
    $image = $this->Gallery_model->find($id)->row();

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('admin/layoutgaleriedit',['image'=>$image]);
    }
    else
    {
        if(isset($_FILES["userfile"]["name"]))
        {
            /* Start Uploading File */
            $config =   [
                            'upload_path'   => './asset/images/',
                            'allowed_types' => 'gif|jpg|png',
                            'max_size'      => 2000,
                            'max_width'     => 640,
                            'max_height'    => 480
                        ];

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('admin/layoutgaleriedit',['image'=>$image,'error'=>$error]);
            }
            else
            {
                    $file = $this->upload->data();
                    $data['file'] = 'asset/images/' . $file['file_name'];
                    unlink($image->file);
            }
        }

        $data['caption']        = set_value('caption');
        $data['description']    = set_value('description');

        $this->Gallery_model->update($id, $data);
        $this->session->set_flashdata('message','Gambar sudah diperbarui..');
        redirect('admin/galeri');
    }
}


public function delete($id)
{
    $this->Gallery_model->delete($id);
    $this->session->set_flashdata('message','Gambar sudah dihapus..');
    redirect('admin/galeri');
}

他给了我们一个使用unlink的提示,但我不知道如何使用它,所以我试着问你所有人的答案。我希望你们都能告诉我学校任务的正确答案

4 个答案:

答案 0 :(得分:1)

您可以在一个功能中删除DB和文件夹中的图像:

public function delete($id)
{
    $image = $this->Gallery_model->find($id)->row();

    // delete image
    if (is_file('image_path/'.$image->file)) {
        unlink('image_path/'.$image->file);
    }

    $this->Gallery_model->delete($id);
    $this->session->set_flashdata('message','Gambar sudah dihapus..');
    redirect('admin/galeri');
}

答案 1 :(得分:0)

控制器中的

public function delete($id)
{
    $image = $this->Gallery_model->find($id)->row();
    $this->Gallery_model->delete($id);
    // use
    delete_files(FCPATH.$image->file);  // codeigniter method to delete file
    // or use php method
    // unlink(FCPATH.$image->file);  // php unlink method
    $this->session->set_flashdata('message','Gambar sudah dihapus..');
    redirect('admin/galeri');
}

答案 2 :(得分:0)

如果(file_exists(FCPATH $图像 - >文件)) {

//Animate on scroll
var offerGrid = new AnimOnScroll( document.getElementById( 'grid' ), {
    minDuration : 0.4,
    maxDuration : 0.7,
    viewportFactor : 0.2
});

// search offers
$('#load-offers-form').submit(function(event) {
    event.preventDefault();
    var postData = $(this).serialize();
    $.post(site_url+'/welcome/loadMoreOffers', postData, function(data) {
        $('#grid').html('');
        var content = genOfferHtml(data);
        $('#grid').html(content);
    });
});

// generate html from data
function genOfferHtml(data){
    var html = '';
    if (data) {
        data = $.parseJSON(data);
        $.each(data, function(i, offer) {
            if (i == 0) {
                html+='<li>';
            }else if(i % 2 == 0){
                html+='</li>\
                <li>';
            }
            html+='<div class="col-sm-6 offer">\
            <a href="">\
            <div class="overlay" style="background-image:url('+baseUrl+offer.offer_image_path+offer.offer_image+')">\
            <div class="offer-hotel">'+offer.hotel_name+'</div>\
            <div class="offer-hotel-location">'+offer.city_name+' ,'+offer.country_name+'</div>\
            <div class="offer-details col-sm-12">\
            <div class="col-sm-9 offer-dates-bg">\
            FROM '+offer.offer_startdate+' TO '+offer.offer_enddate+'\
            </div>\
            <div class="offer-val-bg col-sm-3 pull-right">\
            <div>'+offer.discount+'</div>\
            <div>OFF</div>\
            </div>\
            </div>\
            </div>\
            </a>\
            </div>';
        });         
    }else{
        html+='<li>no data</li>';   
    }
    return html;
}

}

答案 3 :(得分:0)

这是我的删除功能,它可以正常工作

public function delete($id){
$image = $this->M_sizechart->find($id)->row();
 $this->M_sizechart->delete($id);
 if(file_exists(FCPATH.$image->file)) {
   unlink(FCPATH.$image->file);
 }
 $this->session->set_flashdata('message','Image has been deleted..');
 redirect(base_url("sizechart"));
}