首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP glob和unlink

PHP glob和unlink
EN

Stack Overflow用户
提问于 2019-07-11 14:11:23
回答 1查看 100关注 0票数 0

有点奇怪,但我已经知道了如何使用glob来显示图像列表,现在我想在每个图像下添加一个按钮,让您可以单独删除每个图像,我认为我只需要使用unlink,但是每当我尝试它时,似乎只是删除服务器上的每个文件!:‘)

下面是我到目前为止掌握的代码:

代码语言:javascript
复制
<?php
  // This sets the variable $filelist, and get it to search the specificied levels of wildcards for jpg, png, JPG, and PNG using glob:
    $filelist = glob('{*.jpg,*/*.jpg,*/*/*.jpg,*/*/*/*.jpg,*/*/*/*/*.jpg,*/*/*/*/*/*.jpg,*.png,*/*.png,*/*/*.png,*/*/*/*.png,*/*/*/*/*.png,*/*/*/*/*/*.png,*.JPG,*/*.JPG,*/*/*.JPG,*/*/*/*.JPG,*/*/*/*/*.JPG,*/*/*/*/*/*.jpg,*.PNG,*/*.PNG,*/*/*.PNG,*/*/*/*.PNG,*/*/*/*/*.PNG,*/*/*/*/*/*.PNG}', GLOB_BRACE);

  // This filters the above into date order from newest to oldest:
  usort($filelist, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));

  // This is how I now output the data, basically looks for each value of $filelist and sets it as $link, then outputs this and concatenates it with itself as a href and a background-image:
  echo '<div class="thumbnail-grid">';

      if($filelist){
        foreach($filelist as $link){
          echo '<div class="tile-container">';
          echo '<a style="background-image: url('.$link.');" class="photo-link" href="'.$link.'"><i class="fas fa-link"></i></a>';
          // This adds in a delete button:
          echo '<form method="post"><input style="cursor: pointer;" name="delete" type="submit" value="DELETE"></form>  ';
          echo '</div>';
          // This is the script that should be doing the unlinking:
          if(isset($_POST['delete']))
            {
                unlink($link);
            }
        }
      }else{
        echo ' No images found.';
      }

  echo '</div>';

希望这一切都有意义/不要要求太多!

非常感谢杰克。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-11 15:19:52

这并不是很安全,因为任何人都可以发送带有文件名的POST变量并删除它。但是作为一个例子,您可以循环遍历文件,并给delete按钮一个文件名的值,然后在检查posted删除按钮时删除文件。

代码语言:javascript
复制
// This is the script that should be doing the unlinking:
if(isset($_POST['delete'])){
    unlink($_POST['delete']);
}

if($filelist){
    foreach($filelist as $link){
        echo '<div class="tile-container">';
        echo '<a style="background-image: url('.$link.');" class="photo-link" href="'.$link.'"><i class="fas fa-link"></i></a>';
        echo '<form method="post">
        <input style="cursor: pointer;" name="delete" type="submit" value="'.$link.'">
        </form> ';
        echo '</div>';
    }
}else{
    echo ' No images found.';
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56991221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档