PHP 中的基本文件读取问题 /
我有 12 个 XML 文件,从中提取一个 CSV 文件,从中提取第 1 列并将值附加到 tt.txt 文件。
现在,我需要从这个 .txt 文件中提取值...每次将数据写入其中... 但问题是,当我使用
$contents = fread($fd,filesize($filename));
fclose ($fd);
$分隔符 = ',' ;
$splitcontents = 爆炸($delimiter, $contents);
每次附加 tt.txt 文件时,它仅从文件的第一个值读取!
我希望你理解这个问题..我需要的是,我希望 $contents 仅包含附加的新数据...而不是每次都从文件的开头读取...
有没有办法实现这一点,或者 php 失败了?/
这个问题是从TXT文件中提取的->执行计算->写入一个新的txt文件。问题是我无法从中间值读取到新值。PHP 总是从文件的开头读取。
I have a 12 XML files from which I am extracting ONE CSV file, from which - I am extracting column 1 and appending values to a tt.txt file .
NOW, I need to extract the values from this .txt file... everytime data is written to it ...
But the problem is , when I use
$contents = fread ($fd,filesize ($filename));
fclose ($fd);
$delimiter = ',' ;
$splitcontents = explode($delimiter, $contents);
IT reads ONLY from the first value of the file , every time a tt.txt file is appended !
I hope u understand the problem .. What I need is , I want $contents to have only the new data that was appended... instead it reads from the start of the file everytime...
Is there a way to achieve this, or does php fail ?/
This prob is extraction from TXT file- > performing computations- > writing INTO a new txt file . The problem being that I can't read from a middle value to a new value.. PHP always reads from the start of a file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你需要存储最后一个文件的位置。
调用 filesize 来获取当前长度,读取文件,稍后检查文件大小是否不同(或者您可能以其他方式知道这一点,并使用 fseek 移动文件中的光标,然后从那里读取。IE
:
I think you need to store the last file position.
Call filesize to get current length, read the file, later, check if filesize is different (or maybe you know this some other way, and use fseek to move the cursor in the file, then read from there.
IE:
它只读取第一个字段,因为 PHP 不会自动假定换行符 (
\n
) 表示新记录;你必须自己处理这件事。使用您已经拥有的内容,我会执行以下操作:
将来,如果您想逐行进行操作,因为您会冒着通过读取中的整个文件来占用太多资源的风险,使用
fgets()
。It is only reading the first field because PHP does not automatically assume that a newline character (
\n
) means a new record; you have to handle this, yourself.Using what you already have, I would do the following:
In the future, if you want to go line-by-line, as you run the risk of hogging too many resources by reading the entire file in, use
fgets()
.我不擅长数组,但在我看来,您需要一个关联数组(我正在使用以下代码做类似的事情。
$lines =explode("\n", $contents);
I'm not great at arrays but it sounds to me like you need an associative array (I'm doing a similar thing with the following code.
$lines = explode("\n", $contents);