PHP File/Dir Operations

1- Opening and Reading a File Line by Line

a- read Line by Line
<html><body>
<?php
$filename = “test.txt”;
$fp = fopen( $filename, “r” ) or die(“Couldn’t open $filename”);
while ( ! feof( $fp ) ) {
   $line = fgets( $fp, 1024 );
   print “$line<br>”;
}
?>
</body></html>

a- read with fread()

<html><body>
<?php
$filename = “test.txt”;
$fp = fopen( $filename, “r” ) or die(“Couldn’t open $filename”);
while ( ! feof( $fp ) ) {
   $chunk = fread( $fp, 16 );
   print “$chunk<br>”;
}
?>
</body></html>

c- read by char
<html><body>
<?php
$filename = “test.txt”;
$fp = fopen( $filename, “r” ) or die(“Couldn’t open $filename”);
while ( ! feof( $fp ) ){
   $char = fgetc( $fp );
   print “$char<BR>”;
}
?></html></body>

2- Upload: path, file name, size and type

<html>
<?php
$file_dir = “.”; $file_url = “.”;
if ( isset( $fupload ) ){
   print “path: $fupload<br>\n”;
   print “name: $fupload_name<br>\n”;
   print “size: $fupload_size bytes<br>\n”;
   print “type: $fupload_type<p>\n\n”;
   if ( $fupload_type == “image/gif” ){
     copy ( $fupload, “$file_dir/$fupload_name”) or
die (“Couldn’t copy”);
     print “<img
src=\”$file_url/$fupload_name\”><p>\n\n”;
   }
}
?>
<body>
<form enctype=”multipart/form-data” action=”<?php print
$PHP_SELF?>” method=”POST”>
   <input type=”hidden” name=”MAX_FILE_SIZE”
value=”51200″>
   <input type=”file” name=”fupload”>
   <input type=”submit” value=”Send file!”>
</form>
</body></html>