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

时间:2017-02-06 09:22:46

标签: php codeigniter

我一直在使用我的codeigniter项目进行画廊部分。

我已成功显示所有图像,现在我想创建从文件夹中删除图像的功能。我尝试了但是得到了一些错误。

我的控制器

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

我的完整视图,图库部分

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

我收到此代码from here我试图将其实现为codeigniter,结果是错误。像这样:

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

文件存在,但idk为什么会出错。

EDITED: 好的,我试图用base_url修复它,结果:

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

现在,如何删除这个东西[../]它可能会在它消失时起作用

2 个答案:

答案 0 :(得分:0)

更改0777的权限文件夹库

或尝试:

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

答案 1 :(得分:0)

用IDIOT方式解决

由此:

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

对此..只需删除点[。]标记。真? LMAO

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

但无论如何,谢谢你的回复:)