热门角色不仅是灵感来源,更是你的效率助手。通过精挑细选的角色提示词,你可以快速生成高质量内容、提升创作灵感,并找到最契合你需求的解决方案。让创作更轻松,让价值更直接!
我们根据不同用户需求,持续更新角色库,让你总能找到合适的灵感入口。
本提示词专为PHP开发场景设计,能够根据用户指定的文件操作类型和文件名,生成专业、准确且安全的PHP文件操作代码。通过角色扮演和结构化工作流程,确保代码符合最佳实践,包含必要的错误处理和安全性考量,适用于文件读取、写入、追加等多种操作场景,帮助开发者快速实现文件管理功能。
<?php
declare(strict_types=1);
/**
* 安全读取 config/settings.json(PHP 7.4+)
* - 路径校验与白名单扩展名
* - 文件存在性与可读性检查
* - 共享锁(LOCK_SH)避免并发写入造成的脏读
* - 文件大小限制防止内存滥用
* - 严格 JSON 解析(JSON_THROW_ON_ERROR)
*
* 使用方式:
* 1) 作为独立脚本运行:php read_config.php
* 2) 作为库引入:require 'read_config.php'; $config = loadConfig();
*/
const BASE_DIR = __DIR__; // 项目根目录(根据需要调整)
const TARGET_RELATIVE_PATH = 'config/settings.json';
const MAX_FILE_BYTES = 1048576; // 最大读取 1 MiB,可按需调整
const LOCK_TIMEOUT_MS = 2000; // 获取共享锁最大等待时间 2s
/**
* 入口函数:加载并解析配置
* @param string $relativePath 相对 BASE_DIR 的配置路径
* @return array 解析后的配置数组
* @throws RuntimeException|JsonException
*/
function loadConfig(string $relativePath = TARGET_RELATIVE_PATH): array
{
$absolutePath = resolveAndValidatePath(BASE_DIR, $relativePath, ['json']);
$json = readFileWithSharedLock($absolutePath, MAX_FILE_BYTES, LOCK_TIMEOUT_MS);
// 严格解析 JSON,出现语法错误将抛出 JsonException
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
if (!is_array($data)) {
throw new RuntimeException('配置文件内容必须为 JSON 对象(解析结果应为关联数组)');
}
return $data;
}
/**
* 安全解析与校验路径,限制在 baseDir 之下并校验扩展名
*/
function resolveAndValidatePath(string $baseDir, string $relativePath, array $allowedExtensions): string
{
$baseReal = realpath($baseDir);
if ($baseReal === false || !is_dir($baseReal)) {
throw new RuntimeException('基础目录无效或不可用');
}
// 基础的路径注入防护:禁止绝对路径、禁止上级目录跳转、禁止空字节
if ($relativePath === '' ||
$relativePath[0] === DIRECTORY_SEPARATOR ||
strpos($relativePath, "\0") !== false ||
strpos($relativePath, '..') !== false
) {
throw new RuntimeException('相对路径不合法');
}
// 校验扩展名
$ext = strtolower((string)pathinfo($relativePath, PATHINFO_EXTENSION));
if ($ext === '' || !in_array($ext, $allowedExtensions, true)) {
throw new RuntimeException('不被允许的文件类型');
}
// 拼接并解析真实路径
$candidate = $baseReal . DIRECTORY_SEPARATOR . ltrim($relativePath, DIRECTORY_SEPARATOR);
if (!is_file($candidate)) {
throw new RuntimeException('目标文件不存在:' . $relativePath);
}
$real = realpath($candidate);
if ($real === false) {
throw new RuntimeException('无法解析文件真实路径');
}
// 保证文件位于 baseDir 内部
$prefix = rtrim($baseReal, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
if (strpos($real, $prefix) !== 0) {
throw new RuntimeException('访问越界:目标文件不在允许的基础目录内');
}
if (!is_readable($real)) {
throw new RuntimeException('目标文件不可读(请检查权限)');
}
return $real;
}
/**
* 以共享锁安全读取文件
*/
function readFileWithSharedLock(string $absolutePath, int $maxBytes, int $lockTimeoutMs): string
{
$fh = @fopen($absolutePath, 'rb');
if ($fh === false) {
throw new RuntimeException('无法打开文件进行读取');
}
try {
// 获取共享锁,等待直至超时
$start = microtime(true);
$locked = false;
do {
$locked = flock($fh, LOCK_SH | LOCK_NB);
if ($locked) {
break;
}
usleep(10_000); // 10ms
} while (((microtime(true) - $start) * 1000) < $lockTimeoutMs);
if (!$locked) {
throw new RuntimeException('获取文件共享锁超时');
}
// 尺寸检查(如无法获取也继续读)
$size = @filesize($absolutePath);
if (is_int($size) && $size > $maxBytes) {
throw new RuntimeException('文件过大,已超出安全读取限制(' . $maxBytes . ' 字节)');
}
// 读取内容
$content = stream_get_contents($fh);
if ($content === false) {
throw new RuntimeException('读取文件内容失败');
}
return $content;
} finally {
// 释放锁并关闭
if (is_resource($fh)) {
@flock($fh, LOCK_UN);
@fclose($fh);
}
}
}
/* ----------------------- 示例执行逻辑(可根据需要保留/移除) ----------------------- */
if (PHP_SAPI === 'cli') {
// CLI 演示:仅输出加载结果,不回显具体配置以避免敏感信息泄露
try {
$config = loadConfig();
echo "配置加载成功。键数量:" . count($config) . PHP_EOL;
exit(0);
} catch (Throwable $e) {
fwrite(STDERR, '加载配置失败:' . $e->getMessage() . PHP_EOL);
exit(1);
}
} else {
// Web 环境演示:返回通用状态,不暴露配置详情
header('Content-Type: application/json; charset=utf-8');
try {
$config = loadConfig();
echo json_encode(['success' => true, 'message' => '配置加载成功'], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode(['success' => false, 'error' => 'CONFIG_READ_ERROR'], JSON_UNESCAPED_UNICODE);
}
}
<?php
declare(strict_types=1);
/**
* 安全写入(覆盖写入)示例:将内容写入 storage/logs/app.log
* - 原子写入:先写入临时文件,再原子替换目标文件,避免部分写入
* - 权限控制:目录与文件权限设置(0750/0640)
* - 安全检查:固定路径、目录非符号链接、大小限制、内容清洗
* - 兼容:PHP 7.4+
*/
const RELATIVE_TARGET = 'storage/logs/app.log'; // 固定相对路径(不接收用户输入)
const MAX_BYTES = 1024 * 1024; // 单次写入最大1MB
const DIR_PERM = 0750;
const FILE_PERM = 0640;
/**
* 确保目录存在且可写,拒绝使用符号链接目录
*/
function ensureDirectory(string $dir, int $perm = DIR_PERM): void
{
if (is_dir($dir)) {
if (is_link($dir)) {
throw new RuntimeException("Refusing to use symlinked directory: $dir");
}
if (!is_writable($dir)) {
throw new RuntimeException("Directory is not writable: $dir");
}
return;
}
if (!mkdir($dir, $perm, true)) {
throw new RuntimeException("Failed to create directory: $dir");
}
@chmod($dir, $perm); // 受 umask 影响,尽力设置
}
/**
* 日志内容清洗:
* - 尝试转为 UTF-8
* - 统一换行符为 \n
* - 去除不可打印控制字符(保留 \t 和 \n)
* - 限制最大字节数
*/
function sanitizeLogContent(string $input): string
{
if (function_exists('mb_detect_encoding') && function_exists('mb_convert_encoding')) {
if (!mb_detect_encoding($input, 'UTF-8', true)) {
// 当来源编码未知时,'auto' 会尝试检测常见编码并转为 UTF-8
$input = mb_convert_encoding($input, 'UTF-8', 'auto');
}
}
// 统一换行
$input = str_replace(["\r\n", "\r"], "\n", $input);
// 去除不可打印控制字符(保留 \t 与 \n)
$filtered = @preg_replace('/[^\P{C}\t\n]/u', '', $input);
if ($filtered !== null) {
$input = $filtered;
}
if (strlen($input) > MAX_BYTES) {
throw new RuntimeException('Log content exceeds size limit of ' . MAX_BYTES . ' bytes.');
}
return $input;
}
/**
* 原子覆盖写入:
* - 在同目录下创建临时文件,写入完成后 rename 覆盖目标文件
* - 在部分平台上,如目标存在 rename 可能失败,则先尝试 unlink 再重试
*/
function safeOverwrite(string $targetPath, string $data, int $perm = FILE_PERM): void
{
$dir = dirname($targetPath);
ensureDirectory($dir);
$tmp = tempnam($dir, 'wrt_');
if ($tmp === false) {
throw new RuntimeException('Failed to create temporary file.');
}
$bytes = file_put_contents($tmp, $data, LOCK_EX);
if ($bytes === false || $bytes !== strlen($data)) {
@unlink($tmp);
throw new RuntimeException('Failed to write all data to temporary file.');
}
// 原子替换(同一文件系统内)
if (!@rename($tmp, $targetPath)) {
// 某些平台(尤其是旧版 Windows)如目标存在可能失败
if (file_exists($targetPath) && !@unlink($targetPath)) {
@unlink($tmp);
throw new RuntimeException('Failed to replace existing target file.');
}
if (!@rename($tmp, $targetPath)) {
@unlink($tmp);
throw new RuntimeException('Failed to move temporary file into place.');
}
}
// 设置最终文件权限(尽力而为)
@chmod($targetPath, $perm);
}
/**
* 计算目标文件的绝对路径(固定常量,不接受外部输入)
*/
function getTargetAbsolutePath(): string
{
$baseDir = realpath(__DIR__);
if ($baseDir === false) {
throw new RuntimeException('Failed to resolve script base directory.');
}
return $baseDir . DIRECTORY_SEPARATOR . str_replace(['/', '\\'], DIRECTORY_SEPARATOR, RELATIVE_TARGET);
}
/**
* 示例写入(覆盖写入)
* 注意:覆盖写入会清空已有内容,仅保留本次写入内容
*/
try {
// 示例日志内容(应由服务器端生成;若包含用户输入请先清洗/转义)
$logLine = sprintf(
"[%s] level=INFO message=%s\n",
date('c'),
'Application started'
);
$payload = sanitizeLogContent($logLine);
$target = getTargetAbsolutePath();
// 执行覆盖写入
safeOverwrite($target, $payload);
// 按需处理返回(CLI/HTTP)
echo "Wrote log to: " . $target . PHP_EOL;
} catch (Throwable $e) {
// 记录详细错误到 PHP 错误日志(不向客户端暴露具体路径/细节)
error_log('Log write error: ' . $e->getMessage());
// 客户端返回最小错误信息
if (PHP_SAPI !== 'cli') {
http_response_code(500);
}
echo "Failed to write log.";
}
附:如果您更适合“追加写入”日志(常规日志场景),可将 safeOverwrite 替换为使用 FILE_APPEND 的实现(请谨慎使用,这里仅提供参考,不在本次“write/覆盖”范围内):
可能的错误与解决建议:
用一次清晰输入,生成一整套“能直接上线”的PHP文件操作方案:包含可执行代码(读取/写入/追加/日志等)、贴心注释、使用步骤、安全提示与常见错误处理,帮助个人与团队以分钟级速度完成从需求到交付。通过专家级角色与标准化输出,显著降低安全风险与审查成本,统一团队代码风格,减少返工,推动用户快速试用并形成稳定付费使用。
快速产出安全可靠的文件读写与追加模块,内置异常与日志格式,缩短开发、评审与上线周期。
用现成模板搭建配置存储、导入导出与日志落盘,减少踩坑和返工,准时交付稳定版本赢取口碑。
在多任务并行时,一键生成后端文件处理逻辑与示例说明,降低沟通成本,提升联调效率。
将模板生成的提示词复制粘贴到您常用的 Chat 应用(如 ChatGPT、Claude 等),即可直接对话使用,无需额外开发。适合个人快速体验和轻量使用场景。
把提示词模板转化为 API,您的程序可任意修改模板参数,通过接口直接调用,轻松实现自动化与批量处理。适合开发者集成与业务系统嵌入。
在 MCP client 中配置对应的 server 地址,让您的 AI 应用自动调用提示词模板。适合高级用户和团队协作,让提示词在不同 AI 工具间无缝衔接。
半价获取高级提示词-优惠即将到期