File/Directory

How to create New file and open Existing file
The fopen() function is used to create new file and also open existing file.
Syntax:- fopen(“filename”,”mode”);

Mode Description
r read only
w write only
a append

Some Usefull File Handling Function are:
fclose()
This function closes the opening file. Return type bool.
Syntax:-

$file=fopen("myfile.txt","r"); 
fclose($file);

fwrite()
To write the content into the file. Return type int.
Syntax:-

$file=fopen("myfile.txt","w"); 
fwrite($file,'my name is shishir');

fgets()
To read a single line from a file. Return type string.
Syntax:-

$file=fopen("myfile.txt","r"); 
echo fgets($file)

fgetc()
To read a single character from a file. Return type string.

$file=fopen("myfile.txt","r");
 echo fgetc($file);

feof()
This function check end-of-file. if you are at the end of file this function return true otherwise return false.Return type bool.

$file=fopen("myfile.txt","r");
    while(!feof($file))
     { ...... code here 
     }

file_exists()
This function is use to check whether a file or directory exists or not.This function return true if file exists otherwise false. Return type bool.

file_exists("file.txt");

file_size()
This function return the file size in bytes. Return type integer

filesize("file.txt");

Example 1:- PHP script to create new msword file in d: and also write content into the file.

<?php
   $file = fopen("d:\welcome.doc", "w") ;
   fwrite($file,'my name is shishir'); 
  fclose($file);
 ?>

Note:- When we take (w) mode means we are create new file.

Example 2 :- PHP script to open msword file from d: in append mode and also add some content into the file.

<?php
   $file = fopen("d:\welcome.doc", "a") ;
   fwrite($file,'my name is shishir');
  fclose($file);
 ?>

Note:- When we take (a) mode means we open existing file. if file does not exist then (a) mode create new file

Example 3 :- we are checking the MULTIMEDIA directory exist in d: or not.

<?php 
  if(file_exists("d:\MULTIMEDIA")) 
  echo "exists"; else echo "Note exists"; 
?>

Output:- exists

Example 4:- PHP script to open file and read it contain line by line

<?php 
  $file = fopen("welcome.txt", "r") ; 
  while(!feof($file))
   { 
    echo fgets($file). "<br />";
   } 
  fclose($file);
 ?>

Example 5:- PHP script to open file and read it contain character by character.

<?php
    $file=fopen("welcome.txt","r");
      while (!feof($file)) 
        {
         echo fgetc($file); 
        } 
      fclose($file); 
?>

Example 6:- PHP script to read whole file at once

<?php
  if(file_exists("myfile.txt")) 
   {
     $str=file_get_contents("myfile.txt");
     $text=str_replace("\n","<br>",$str);
     echo $text; 
   }
 ?>

Example 7:- PHP script to write whole file at once

<?php
   $str_text="This\nis\ntext\nexample"; 
   file_put_contents("myfile.txt",$str_text); 
?>

Directory in PHP
There are some function to handle directory.
opendir():- To open any directory.

readdir():- To all file stored in directory.

closedir():- To close any directory.

Example 8:- By this example we are open directory and print files name

<?php
    $handle = opendir('upload'); 
         if($handle) 
             {
             while($file = readdir($handle))
               { 
                if ($file != "." && $file != "..")
                   {
                    echo "$file\n"."<br>"; 
                   }
               }
    closedir($handle);
             }
 ?>


anime porn
Old Fashion Magic Cookie Bars
youjizz two snacks and two meals

Shania Twain and Jennifer Lopez compete in a fashion face
free black porn commonly known as H

notice of pendency of action NY
anime porn time travelers and ESPers were going to be over

True Religion Apparel’s CEO Discusses Q4 2012 Results
porno Car salesmen must have good communication

Telephone Music Video with Beyonce
porn they wore dresses that were longer depending on their age

What to Pair With Cheetah Print Pumps
porno Rough heels won’t look good in a backless sandal

Fashion Eyeglasses for Trendy Professionals
transformice according to their rank and what they could afford

Secret of Hair Extensions Revealed
rape porn 18th Century Wigs Hats for Men
This entry was posted in   PHP.
Bookmark the   permalink.

Admin has written 171 articles