miércoles, 11 de enero de 2017

reading files with fread and fgets

<?php

$file=fopen("/root/list.txt","r");
$arr=array();
while(!feof($file)){

$list=fgets($file);

$arr[]=$list;

}

fclose($file);
echo $arr[0];
?>
this will print  11111 ( first line of are list file)





<?php

$file=fopen("/root/list.txt","r");
$arr=array();
while(!feof($file)){

$list=fread($file,9999);

$arr[]=$list;

}

fclose($file);
echo $arr[0];
?>
this  print all values on  the file because  the amount of data is smaller than 9999

11111
22222
33333
44444


If we use fgets with no parameter   each line will be a different variable.

if we use fread all values  will be one variable if  are smaller than the number of byte specif on the parameter


Also using  File()

<?php

print_r(file("list.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));

?>

Array
(
    [0] => 101
    [1] => 102
    [2] => 104
    [3] => 104
)

No hay comentarios:

Publicar un comentario