Reading files in PHP:
~ddailey/fileread.php Simplest way? (Small files) reading an entire file into an array <?
$f="words";
$o=file($f);
echo "./$f has ".count($o)." lines<br>";
print "The first, 2000th and last lines:<br>";
print $o[0]."<br>";
print $o[2000]."<br>";
print $o[count($o)-1]."<br>";
?>~ddailey/php/check.php Printing all lines and numbers through a key-value "hash." <?
$f="testing";
$o=file($f);
echo "./$f has ".count($o)." lines<br>";
while ($i = each($o)){
$k=$i['key'];
$v=$i['value'];
print "$k.$v<br>";
}
?>~ddailey/mappy.php Printing lines that match a regular expression. <?
$f="afile";
$o=file($f);
$a=preg_grep("/a/",$o);
print_r($o);
echo "<hr>";
foreach ($a as $key => $value){
echo $key.":".$value."<br>";
}
?>~ddailey/php/ten.php opening files <html><body>
<div align="center">
<b>various tricks</b><br>
opening and displaying a file
(that has html and php in it)
</div>
<form><input size=80 value='
<? include "testing";
//we dare not include "ten.php"
//since it will recursively expand the file
?>'></form>
<?
$f=fopen("ten.php", "r");
$data=htmlentities(fread($f,1000));
print "<pre>$data</pre>"
?>
<form><textarea cols=70 rows=10>
<? print $data; ?>
</textarea>
</body></html>~ddailey/tena.php Reading arbitrary chars <html><body>
<div align="center">
<b>reading chars from a file</b><br>
</div>
<?
$file="words";
print "file is named".$file;
$f=fopen($file, "r")
or die ("couldn't open $file");
print "file has been opened";
$s=$num=floor(40000*rand()/getrandmax());
fseek( $f, $s);
$chunk=fread($f,50);
print "<pre>$s</pre>"
?>
<form><textarea cols=70 rows=10>
<? print $chunk; ?>
</textarea>
</body></html>~ddailey/hold/eleven.php reading a random block of characters from a file and playing with them The source code (visible from a link on
the page) uses the following conventions:to determine file size:$fz=filesize( $file );to grab a random 50 characters:$s=$num=floor($fz*rand()/getrandmax());
fseek( $f, $s);
$chunk=fread($f,50);to get rid of carriage returns:$unchunk=ereg_replace("\n",":",$chunk);To find consonant clusters:$w=split("[aeiou:]",$unchunk);To walk through the resultant array:foreach ($w as $i) {
$sl=strlen($i);
print "$i:";
if ($sl>$mx)
{
$mx=$sl;
$remember=$i;
}
}~ddailey/hold/twelve.php reading lines from the beginning of a file (See full source through link on page.)
<textarea name="t" cols=30 rows=15>
<? $file="../words";
$f=fopen($file, "r")
or die ("couldn't open $file");
$c=0;
if ($happy<50){
while ( $c < $happy){
$chunk=fgets($f,100);
print "$c-th line is $chunk";
$c++;
}
}
else
print "$happy is bigger than"
print " I wish to deal with";
?>
</textarea>
Writing a file
Notes: Due to (very) recent changes in the way PHP works under Linux, appending to files is no longer so easy.
Here's what you have to do. While logged into your granite account (let's say your name is abc1234) issue this command:
setfacl -R -d -m apache:rwx /homes/abc1234/public_html
setfacl -R -d -m u:apache:rwx /homes/abc1234/public_html
touch filename
Take a look at this example. We will talk about it in class.
~ddailey/hold/thirteen.php appending to a file
(under repair)Key commands:
if (isset($ws)){
$ws=$ws."\n";
$outf="happychunks";
$o=fopen($outf, "a") or die ("couldn't open $outf");
fwrite($o,$ws);
}