1

Solved! I used an existing json file that I was using to display diagrams on the page.

I have a 2000 line text file. I want to read in the first 3000 bytes to a php variable. This works, but only at the cost of reading in the entire text file:

$little_diagrams = ('assets/diagrams.txt'); $mason = file($little_diagrams);

I tried this, but it doesn't work. Any ideas on why?

$little_diagrams  = file_get_contents("assets/diagrams.txt", NULL, NULL, 0, 3000);

$mason = file($little_diagrams); 

The trouble is that I have to process lines in "assets/diagrams.txt" such as: 2833|6979|Poloskov|||Nikolayev|Igor|2272|1n3rk1/3p1ppp/5q2/2p1P3/2B2P2/r2Q2P1/1b2N2P/1R3K1R|

2832|6979|Poloskov|||Nikolayev|Igor|2272|r2qk2r/1b1p1ppp/n4b2/2pN4/2B1P3/8/PP3PPP/R2QK1NR| 2831|6978|Nikolayev|Igor|2272|Buturin|Vladimir (IM)|2405|r3r1k1/1ppb1pp1/3p1n1p/2nP4/p3P3/4NP2/PPBN1KPP/R3R3| 2830|6978|Nikolayev|Igor|2272|Buturin|Vladimir (IM)|2405|r2qr1k1/1ppb1pp1/p1np1n1p/8/3PP3/4NN2/PPB2PPP/R2Q1RK1|

2829|6977|Nikolayev|Igor|2272|Tabatadze|Tamaz|2288|2rqk2r/4bp1p/p1n1b3/3pP3/Pp1P2p1/1P3p2/1B2NPPP/2RQNRK1|

2828|6976|Lutsko|Igor|2307|Nikolayev|Igor|2272|6r1/1pp2p1k/p2p3p/2bP4/2P2r2/1P4NP/P2R1P1K/5R2|

NEW CODE: (doesn't work, no diagrams are displayed at http://communitychessclub.com/ left column bottom)

$filename = "assets/diagrams.txt";
$handle = fopen($filename, "r");
$little_diagrams = fread($handle, 3000);  //<<--- as per your need 

fclose($handle);


$X = 5000; $line = 0;

foreach($little_diagrams as $line) {$X++; if ($X >= 5040) {break;} $token = explode("|", $line); //etc

}
verlager
  • 794
  • 5
  • 25
  • 43
  • Please define _"it doesn't work"_ – Phil Aug 31 '18 at 04:13
  • The variable $mason echos as nothing. I need it as a data text file to print diagrams. – verlager Aug 31 '18 at 04:16
  • Does the file exist at that path? Try `is_readable('assets/diagrams.txt')`. Also make sure you can [see any errors](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php). `file_get_contents()` returns `false` if something goes wrong – Phil Aug 31 '18 at 04:17
  • Urgh, I see what's wrong now. Just assign `$mason = file_get_contents(...`. You are attempting to run the file content string through `file()` which is not a file path. This will not work – Phil Aug 31 '18 at 04:22
  • read this article, https://www.sitepoint.com/performant-reading-big-files-php/ – Bilal Ahmed Aug 31 '18 at 04:24
  • Possible duplicate of [How to get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – Phil Aug 31 '18 at 04:24
  • @BilalAhmed I don't think OP needs to worry about reading <3kb of file contents – Phil Aug 31 '18 at 04:26
  • _"file_get_contents() reads it in as one line"_ <- no it doesn't. Are you looking at the result in an HTML document by chance? – Phil Aug 31 '18 at 04:42
  • 3000 bytes seems quite arbitrary give you appear to be parsing delimited, structured data. I suggest you have a look at [`fgetcsv()`](http://php.net/manual/function.fgetcsv.php) – Phil Aug 31 '18 at 05:07

1 Answers1

5
<?php
   $filename = "c:\\files\\yourfile.txt";
   $handle = fopen($filename, "rb");
   //$little_diagrams = fread($handle, filesize($filename));
   $little_diagrams = fread($handle, 3000);  //<<--- as per your need 

   fclose($handle);
?>

Try above code and read this fread function documentation

I hope this will help also see the example code Example #2 Binary fread() example

fread takes two arguments

string fread ( resource $handle , int $length )

second is length part

length bytes have been read as per documentation

fread() reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met:


UPDATED BELOW

Note: You can use file_get_contents() to return the contents of a file as a string.

change second argument to FALSE from NULL then try hope it will work

so in my opinion correct code will be like below take a try

<?php
                                         //however working with null also
    $file_content =file_get_contents('demoTest.txt',FALSE,NULL,0,3000);

    echo 'File Size: '.filesize('demoTest.txt');
    echo '<br/> CONTENT HERE<br />'.$file_content;

    echo '<br /><br />String Length: '.strlen($file_content);
?>

read the documentation at here for file function

enter image description here

Er. Amit Joshi
  • 611
  • 5
  • 21
  • Your first example is fine but your _"UPDATED BELOW"_ part contains the same error that OP has; `file()` takes a file-path, not the contents of a file – Phil Aug 31 '18 at 04:52
  • @Phil Thanks For clarification. You are right. Response updated. – Er. Amit Joshi Aug 31 '18 at 05:04
  • Still looks broken to me. It should be `$mason = file_get_contents("assets/diagrams.txt", FALSE, NULL, 0, 3000);`. There is absolutely no use for `file()` in this at all – Phil Aug 31 '18 at 05:05
  • 1
    _"`// it dosent; work you are right phil`"_ <- If it doesn't work, don't put it in your answer; you'll only confuse people. Nice update and example though. I'd upvote again if I could ;) – Phil Aug 31 '18 at 05:18
  • Sorry @Phil but this time image output is attached – Er. Amit Joshi Aug 31 '18 at 05:19