doushi6932 2017-02-06 09:22
浏览 50
已采纳

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

I've been working with my codeigniter project for gallery part.

I've succeed on displaying all images, now I want to make function to delete images from folder. I tried it but get some error.

My Controllers

function delete_img(){
    if (array_key_exists('delete_file', $_POST)) {
      $filename = $_POST['delete_file'];
      if (file_exists($filename)) {
        unlink($filename);
        echo 'File '.$filename.' has been deleted';
      } else {
        echo 'Could not delete '.$filename.', file does not exist';
      }
    }
}

My Full Views, Gallery Part

<?php
    //FOLDER GALLERY-PART
     $dirname = "./assets/images/gallery/";
     $images = glob($dirname."*.jpg");

     array_multisort(array_map('filemtime', $images), SORT_NUMERIC, SORT_DESC, $images);

     foreach($images as $image) {

    "<div class='col-xs-6 col-sm-4 col-md-3 portfolio-item logos'>
       <div class='portfolio-wrapper'>
         <div class='portfolio-single'>
           <div class='portfolio-thumb'>
             <img src='.$image.' class='img-responsive' alt=''>
               </div>
                <div class='portfolio-view'>
                   <ul class='nav nav-pills'>
                     <li><a href='gallerydetail'><i class='fa fa-link'></i></a></li>
                     <li><a href='.$image.' data-lightbox='example-set'><i class='fa fa-eye'></i></a></li>
                     <li><a href='#'><i class='fa fa-heart'></i></a></li>
                        </ul>
                </div>
              </div>
              <div class='portfolio-info'>
                    <form id='imgtitle' class='kart' action='" . base_url() . "gallery/delete' method='post'>
                        <input type = 'hidden' name='delete_file' value='.$image.' />
                        <input type = 'submit' value='Delete' class='btn btn-common'/>
                    </form>
                </div>
            </div>
        </div>";
            }
        ?>

I get this code from here I tried to implement this into codeigniter, the result was error. Like this:

Could not delete ../assets/images/gallery/hermesed231486375164.jpg., file does not exist

The file was exist, but idk why it errors.

EDITED: Ok I was trying to fixed it with base_url, the result:

Could not delete http://k-art.local/../assets/images/gallery/hermesed231486375164.jpg., file does not exist

Now, how to delete this thing [../] it might worked when it disappear

  • 写回答

2条回答 默认 最新

  • douyun1950 2017-02-06 10:11
    关注

    SOLVED WITH IDIOT WAY

    From this:

    <input type = 'text' name='delete_file' value='.$image.' />
    

    To this.. Just deleting the dot[.] mark. Really? LMAO

    <input type = 'text' name='delete_file' value='$image' />
    

    But anyway thanks for replying :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同