簡單的php實現圖片刪除,適用thinkphp及tinkcmf的框架
html代碼
<admintpl file="header" />
<body>
<style>
.show_img{
float: left;
height: 150px;
width: 200px;
text-align: center;
position: relative;
margin: 5px;
color: #fff;
border: 1px solid blue;
}
.show_img .img{
width: 100%;
height: 100%;
display: block;
}
.show_img .img span{
display: block;
position: absolute;
bottom: 0;
width: 100%;
height: 30px;
line-height: 30px;
background-color: rgba(68, 63, 63, 0.59);
}
.show_img .delete{
position: absolute;
top: 0;
right: 0;
border-radius: 50%;
height: 30px;
width: 30px;
line-height: 30px;
background-color: red;
color: #fff;
}
</style>
<div class="wrap js-check-wrap">
<if condition="$page_index">
<ul class="nav nav-tabs">
<li><a href="javascript:history.go(-1)">{:L('UP_PAGE')}</a></li>
<li><a href="{:U('File/index')}">{:L('INDEX_PAGE')}</a></li>
</ul>
<else />
</if>
{$file}
</div>
<script src="__PUBLIC__/js/common.js"></script>
</body>
</html>
php後台代碼
<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
class FileController extends AdminbaseController
{
public function index()
{
$dir = $_GET['file'];//獲取當前頁數
$page_index = true;
if (!$dir) {
$dir = './data/upload';
$page_index = false;
}
//調用處理函數
$list = scandir($dir);
$file_dir = '';
$file_img = '';
foreach ($list as $file) {
$location_dir = $dir . '/' . $file;
if (!(is_dir($location_dir) && '.' != $file && '..' != $file)) {
list($filesname, $kzm) = explode(".", $file);//獲取擴展名
if ($kzm == "gif" or $kzm == "jpg" or $kzm == "JPG") { //文件過濾
$url = U('控制器路徑不方便,填上自己的就ok/delete');
$p = 'address=' . $dir . "/" . $file;
if (strpos($url, '?')) {
$url = $url . '&' . $p;
} else {
$url = $url . '?' . $p;
}
$file_img = $file_img . "<div class='show_img'><a class='img' target=\"_blank\" href=" . $dir . "/" . $file . ">
<img width=150 height=110 src=" . $dir . "/" . $file . "><span>" . $file . "</span></a>
<a class='delete' href=" . $url . ">刪除</a></div>";
}
} else {
$url = U('控制器路徑不方便,填上自己的就ok/index');
$p = 'file=' . $dir . "/" . $file;
if (strpos($url, '?')) {
$url = $url . '&' . $p;
} else {
$url = $url . '?' . $p;
}
$file_dir = $file_dir . "<div class='show_img'>
<a class='img' href=" . $url . ">
<img width=150 height=110 src='/admin/themes/simplebootx/Public/assets/images/folder.jpg'><span>" . $file . "</span></a></div>";
}
}
$this->assign("file", $file_dir . $file_img);
$this->assign("page_index", $page_index);
$this->display();
}
/**
* 刪除
*/
function delete()
{
$address = $_GET['address'];//獲取當前頁數
$result = @unlink($address);
if ($result == false) {
$this->success("刪除成功!");
} else {
$this->error("刪除失敗!");
}
}
private function loopFun($dir)
{
//取出文件或者文件夾
$list = scandir($dir);
foreach ($list as $file) {
$location_dir = $dir . '/' . $file;
list($filesname, $kzm) = explode(".", $file);//獲取擴展名
if ($kzm == "gif" or $kzm == "jpg" or $kzm == "JPG") { //文件過濾
if (!is_dir('./' . $file)) { //文件夾過濾
echo '<br />';
echo $dir . '/' . $file;
echo "<a href=" . $dir . "/" . $file . "><img width=150 height=110 src=" . $dir . "/" . $file . "></a><br>";
$array[] = $file;//把符合條件的文件名存入數組
$i++;//記錄圖片總張數
}
}
//判斷是否是文件夾 是就調用自身函數再去進行處理
if (is_dir($location_dir) && '.' != $file && '..' != $file) {
$this->loopFun($location_dir);
}
}
}
public function load_image($dir, $menuid)
{
$list = scandir($dir);
foreach ($list as $file) {
$location_dir = $dir . '/' . $file;
if (!(is_dir($location_dir) && '.' != $file && '..' != $file)) {
list($filesname, $kzm) = explode(".", $file);//獲取擴展名
if ($kzm == "gif" or $kzm == "jpg" or $kzm == "JPG") { //文件過濾
echo '<br />';
echo $dir . '/' . $file;
echo "<a href=" . $dir . "/" . $file . "><img width=150 height=110 src=" . $dir . "/" . $file . "></a><br>";
}
} else {
echo "<a href=" . U('控制器路徑不方便,填上自己的就ok/index') . '&file=' . $dir . "/" . $file . ">" . $file . "</a><br>";
}
}
}
}
*