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>

One Response to “PHP File/Dir Operations”

  1. Ishita, 18. December 2015, 2:34

    I have been wondering lalety, after watching a Theroux documentary about black nationalists about how do black people feel about white people fighting for black rights. I can imagine some being offended as it really shows that white people have enough rights to fight for somebody else and other black people liking it because it might give them more chance in life. This may sound weird, but I am directing this to question to black people as I would just like to hear your opinion on this. Thanks.

Post your comment

*

code

 

 

See more website here...

  • Odlinks
  • Farsi-News
  • Advertise Here