简体   繁体   English

PHP中的基本fileread问题

[英]Basic fileread question in 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 . 我有12个XML文件,从中提取一个CSV文件,从中-我提取第1列并将值附加到tt.txt文件。

NOW, I need to extract the values from this .txt file... everytime data is written to it ... But the problem is , when I use 现在,我需要从此.txt文件中提取值...每次将数据写入其中时...问题是,当我使用

$contents = fread ($fd,filesize ($filename)); $ contents = fread($ fd,filesize($ filename));
fclose ($fd); fclose($ fd);
$delimiter = ',' ; $ delimiter =',';
$splitcontents = explode($delimiter, $contents); $ splitcontents = explode($ delimiter,$ contents);

IT reads ONLY from the first value of the file , every time a tt.txt file is appended ! 每当添加tt.txt文件时,它仅从文件的第一个值读取!

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... 我希望您能理解问题..我需要的是,我希望$ contents仅具有附加的新数据...而是每次都从文件的开头读取...

Is there a way to achieve this, or does php fail ?/ 有没有一种方法可以做到这一点,或者php是否失败?/

This prob is extraction from TXT file- > performing computations- > writing INTO a new txt file . 此问题是从TXT文件中提取的,然后执行计算,然后将其写入新的txt文件。 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. 问题是我无法从中间值读取到新值。PHP始终从文件开头读取。

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); 我不是很擅长数组,但听起来像我需要一个关联数组(我在下面的代码中做类似的事情。$ 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 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. 调用filesize获取当前长度,读取文件,稍后,检查filesize是否不同(或者您可能以其他方式知道这一点,然后使用fseek在文件中移动光标,然后从那里读取。

IE: 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;

It is only reading the first field because PHP does not automatically assume that a newline character ( \\n ) means a new record; 它仅读取第一个字段,因为PHP不会自动假定换行符( \\n )表示新记录; 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() . 将来,如果您想逐行浏览,可能会因读取整个文件而浪费太多资源,请使用fgets()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM