PHP 删除文件接口

amao
<?php
//设置数据格式-- json
header('content-type:application/json;charset=utf-8');
// *代表允许任何网址请求
header('Access-Control-Allow-Origin:*');
// 允许请求的类型
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE');
// 设置允许任何请求头的字段
header('Access-Control-Allow-Headers:*');
//屏蔽参数为空警告  
error_reporting(0);


$fileName = isset($_POST['fileName']) ? $_POST['fileName'] : ''; //图片文件名

if ($fileName == '') {
    $res = array('code' => '0', 'msg' => '参数错误');
    exit(json_encode($res));
} else {
    delImg($path = '../assets/img/', $fileName);
}

function delImg($path, $fileName)
{
    if ($path == '') {
        $res = array('code' => '0', 'msg' => '路径有误');
        exit(json_encode($res));
    } else {
        $status = unlink($path . $fileName);
        if ($status) {
            $res = array('code' => '1', 'msg' => '删除成功');
            exit(json_encode($res));
        } else {
            $res = array('code' => '-1', 'msg' => '删除失败');
            exit(json_encode($res));
        }
    }
}

请求示例:
http://localhost/api/imgAPI/delImg.php?fileName=1576812861.jpg

阅读 1.2k

不学无术的大三狗一只

25 声望
2 粉丝
0 条评论

不学无术的大三狗一只

25 声望
2 粉丝
文章目录
宣传栏