我是php的新手。只是想运行这个本地网站。尝试从每5分钟更新一次的txt文件中提取最后5个字符。这是我的脚本:
<?php
$temperature = include( "temperaturelog.txt");
$temp = substr($temperature, -5);
print $temp
?>
这是我的测试文件:
14.75
13.52
以下是我的网页显示的内容:
关于我做错了什么有什么线索吗?或者误解了文档?
发布于 2018-07-24 07:46:59
使用file_get_contents('fileName')
或fopen('fileName', 'mode'); fread('fileName', length);
第一种方法:
将PHP代码替换为以下代码:
<?php
$temperature = file_get_contents( "temperaturelog.txt");
$temp = substr($temperature, -5);
print $temp;
?>
你可以在这里找到更多关于file_get_contents()
的信息:http://php.net/manual/en/function.file-get-contents.php
第二种方法:
<?php
$fileName = "temperaturelog.txt";
$handle = fopen($fileName , 'r');
$temperature = fread($handle, filesize($fileName));
$temp = substr($temperature, -5);
print $temp;
?>
对于第二种方法,更多信息:http://php.net/manual/en/function.fopen.php
https://stackoverflow.com/questions/51488491
复制相似问题