thinkphp6入门(21)-- 如何删除图片、文件

假设文件的位置在

php 复制代码
/**
 * 删除文件
 * $file_name = 'avatar/20240208/d71d108bc1086b498df5191f9f925db3.jpg'
 */
function deleteFile($file_name)
{
    // 要删除的文件路径
    $file = app()->getRootPath() . 'public/uploads/' . $file_name; 
    
    $result = [];
    
    if (is_file($file)) 
    {
        if (@unlink($file)) 
        {
            $result['status'] = 1;
            $result['msg'] =  "文件已成功删除!";
        } 
        else 
        {
            $result['status'] = -1;
            $result['msg'] =  "无法删除该文件!";
        }
    } 
    else 
    {
        $result['status'] = -2;
        $result['msg'] =  "指定的文件不存在!";
    }
    
    return $result;
}

因为这个删除函数是通用的,因此可以把它放到公共函数里

by: 软件工程小施同学

相关推荐
云游云记3 小时前
FastAdmin 路由完全开启教程:去掉 index 前缀 + 优雅路由配置
thinkphp
kertag5 天前
ThinkPHP 8 多应用入口绑定:BIND_MODULE vs $http->name() 全面解析
php·thinkphp
妙码生花16 天前
全新的 TP8+Workerman+BuildAdmin 整合方案,已有近 2000 次下载使用。
websocket·php·thinkphp
quweiie18 天前
在php8.3下签到、签退打卡的实现
thinkphp·签到·nesbot/carbon
天宁2 个月前
Workerman + ThinkPHP 8 结合使用
php·thinkphp
云游云记2 个月前
ThinkPHP 队列扩展 (topthink/think-queue) 使用笔记
php·thinkphp·think-queue
用户14644605033793 个月前
PHP 多维数组处理利器:array_column() 用法详解
php·thinkphp
用户3074596982073 个月前
ThinkPHP 6.0 多应用模式下的中间件机制详解
后端·thinkphp
行思理4 个月前
小游戏系统提供二开服务
layui·游戏程序·小游戏·thinkphp
xmode4 个月前
常用自定义函数laravel版+thinkphp版
后端·php·laravel·thinkphp