PHP 写出临时静态html文件
以下是一个简单的 PHP 代码示例,用于创建临时静态 HTML 文件:
<?php
// 定义 HTML 内容
$html = "<html><head><title>临时静态 HTML 文件</title></head><body><h1>Hello, World!</h1></body></html>";
// 创建临时文件
$temp_file = tempnam(sys_get_temp_dir(), 'html');
// 打开文件,写入 HTML 内容
$file = fopen($temp_file, 'w');
fwrite($file, $html);
fclose($file);
// 输出文件内容
echo file_get_contents($temp_file);
// 删除临时文件
unlink($temp_file);
?>
在这个示例中,我们首先定义了要写入文件的 HTML 内容。然后,使用 PHP 的 tempnam() 函数创建一个临时文件,指定目录和文件前缀。我们打开临时文件,并使用 fwrite() 函数将 HTML 内容写入文件中。最后,我们使用 file_get_contents() 函数读取临时文件的内容,并将其输出到屏幕上。最后,我们使用 unlink() 函数删除临时文件。
原文地址: https://www.cveoy.top/t/topic/EWA 著作权归作者所有。请勿转载和采集!