PHP 中的基本文件读取问题 /

发布于 2024-10-27 05:47:46 字数 539 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛溺水了 2024-11-03 05:47:46

我认为你需要存储最后一个文件的位置。

调用 filesize 来获取当前长度,读取文件,稍后检查文件大小是否不同(或者您可能以其他方式知道这一点,并使用 fseek 移动文件中的光标,然后从那里读取。IE

$previousLength = 0;

// your loop when you're calling your new read function
$length = filesize($filename);
fseek($fd,$previousLength);
$contents = fread($fd,$length - $previousLength);
$previousLength = $length;

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:

$previousLength = 0;

// your loop when you're calling your new read function
$length = filesize($filename);
fseek($fd,$previousLength);
$contents = fread($fd,$length - $previousLength);
$previousLength = $length;
此刻的回忆 2024-11-03 05:47:46

它只读取第一个字段,因为 PHP 不会自动假定换行符 (\n) 表示新记录;你必须自己处理这件事。

使用您已经拥有的内容,我会执行以下操作:

$contents = fread($fd, filesize($filename));
close($fd);

/* Now, split up $contents by newline, turning this into an array, where each element
 * is, in effect, a new line in the CSV file. */
$contents = explode("\n", $contents);

/* Now, explode each element in the array, into itself. */
foreach ($contents as &$c) {
   $c = explode(",", $c);
}

将来,如果您想逐行进行操作,因为您会冒着通过读取中的整个文件来占用太多资源的风险,使用 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:

$contents = fread($fd, filesize($filename));
close($fd);

/* Now, split up $contents by newline, turning this into an array, where each element
 * is, in effect, a new line in the CSV file. */
$contents = explode("\n", $contents);

/* Now, explode each element in the array, into itself. */
foreach ($contents as &$c) {
   $c = explode(",", $c);
}

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().

人生戏 2024-11-03 05:47:46

我不擅长数组,但在我看来,您需要一个关联数组(我正在使用以下代码做类似的事情。
$lines =explode("\n", $contents);

foreach ($lines as $line) {

 $parts = explode(',', $line);
    if (count($parts) > 0) {
           $posts = array();
        $posts[] = array('name' => $parts[3],'email' => $parts[4],'phone' =>           $parts[5],'link' => $parts[6],'month' => $parts[0],'day' => $parts[1],'year' =>    $parts[2]);      }


     foreach ($posts as $post):
            $post = array_filter(array_map('trim', $post));

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);

foreach ($lines as $line) {

 $parts = explode(',', $line);
    if (count($parts) > 0) {
           $posts = array();
        $posts[] = array('name' => $parts[3],'email' => $parts[4],'phone' =>           $parts[5],'link' => $parts[6],'month' => $parts[0],'day' => $parts[1],'year' =>    $parts[2]);      }


     foreach ($posts as $post):
            $post = array_filter(array_map('trim', $post));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文