有点奇怪,但我已经知道了如何使用glob来显示图像列表,现在我想在每个图像下添加一个按钮,让您可以单独删除每个图像,我认为我只需要使用unlink,但是每当我尝试它时,似乎只是删除服务器上的每个文件!:‘)
下面是我到目前为止掌握的代码:
<?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>';希望这一切都有意义/不要要求太多!
非常感谢杰克。
发布于 2019-07-11 15:19:52
这并不是很安全,因为任何人都可以发送带有文件名的POST变量并删除它。但是作为一个例子,您可以循环遍历文件,并给delete按钮一个文件名的值,然后在检查posted删除按钮时删除文件。
// 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.';
}https://stackoverflow.com/questions/56991221
复制相似问题