Files

55 lines
1.6 KiB
PHP

<?php
/**
* 文件删除接口(同时删除物理文件) POST /api/document/delete.php
* 入参:id(或 ids 逗号分隔批量)
*/
require_once __DIR__ . '/../common/db.php';
require_once __DIR__ . '/../common/response.php';
require_once __DIR__ . '/../common/auth.php';
require_once __DIR__ . '/../common/logger.php';
checkAjax();
checkAnyPermission(['document', 'competitor_data']);
$id = (int)($_POST['id'] ?? 0);
$ids = trim($_POST['ids'] ?? '');
$idList = [];
if ($id > 0) $idList[] = $id;
if ($ids !== '') {
foreach (explode(',', $ids) as $v) {
$v = (int)trim($v);
if ($v > 0) $idList[] = $v;
}
}
$idList = array_unique($idList);
if (!$idList) {
Response::error('参数错误', 400);
}
$pdo = DB::getInstance()->getPdo();
$in = implode(',', array_fill(0, count($idList), '?'));
// 取出物理路径
$stmt = $pdo->prepare("SELECT id, storage_path FROM company_documents WHERE id IN ($in)");
$stmt->execute($idList);
$docs = $stmt->fetchAll();
// 删除物理文件(仅限本站上传目录内的文件)
foreach ($docs as $doc) {
$path = $doc['storage_path'];
if (strpos($path, 'static/uploads/documents/') === 0) {
$full = __DIR__ . '/../../' . $path;
if (is_file($full)) {
@unlink($full);
}
}
}
// 删除记录(document_links 级联删除)
$del = $pdo->prepare("DELETE FROM company_documents WHERE id IN ($in)");
$del->execute($idList);
$affected = $del->rowCount();
logCurrent('delete', 'document', 'company_documents', null, ['ids' => $idList]);
Response::success(['affected' => $affected], "已删除 $affected 个文件(含物理文件)");