The loop is run through twice.
First $chunk ="Jack" and the file pointer is at sitting at the end of the line. But it doesn't yet know this is also the end of the file, because it hasn't tried reading any further. So the loop is run a second time.
This time, fread fails and returns false, so that's what chunk is set to. And now the file pointer has seen the end of the file. So the loop ends.
By the time you leave the loop, $chunk is false, and false doesn't contain a J.
<?php
$filename = "jack.txt"; // This file reads only one word 'Jack'
$fp = fopen( $filename , 'r' ) or die ("Could not open $filename");
$filesize = filesize($filename);
while ( ! feof( $fp ) ) {
$chunk = fgets ( $fp, $filesize);
var_dump($chunk);
}
if ( preg_match("/J/", $chunk, $array) )
{
print $array[0]; //It doesn't pick anything up?
}
?>
The appropriate solution (drop the loop, use file(), use string catenation, or whatever) depends on what you're trying to do.