KEMBAR78
Chap 5 php files part 1 | PPTX
Internet
programming-1
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 1
Chap 5 files & directories
What we learn?
5.1 Working with files and directories
5.2 Opening and Closing, Getting information about file, Read/write to file, Splitting name
and path from file, Rename and delete files
5.3 Reading and writing characters in file
5.4 Reading entire file
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 2
1)Introduction:-
Working with files and directories
2 types of file-
1. Text file
2. Binary file
3. 3 steps-
1)Open file
2)read/ write file(operation)
3) Close file
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 3
5.2 Opening and Closing
1)opening file-
Fopen()
2)closing file-
fclose()
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 4
Fopen()
•The fopen() is used to open a file, returning a file handle associated with the opened file.
•It can take two or three arguments: filename, mode, and the optional use_include_path.
•The file handle also can be used it to detect whether the file opened or not, if it is opened then file
handle is a positive integer, if not, the file handle is zero.
1. $fp = fopen(“./data.txt", "r"); if(!$fp) die ("Cannot open the file");
./data.txt points to the data.txt in the same directory.
We can pass relative or absolute path.
2.if(!($fp = fopen("http://www.example.com/index.html", "r"))) die ("Cannot open the file.");
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 5
Second parameter is mode
r Open, file for reading only. Sets the file pointer at the beginning of file.
r+ Open file for reading and writing.
w file or writing only. If file is exist then content will be lost. Otherwise
new file is created.
w+ Open file for reading and writing.
a Open file for appending only. If file is exist then contents will be appended to
end of file, if file is not exist then new file is created.
a+ Open file for reading and appending.
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 6
Third (optional) parameter is use_include_path.
•If use_include_path is set to 1, and the filename isn't specified as a relative or absolute path, the
function searches for the file specified by filename first in current directory,
•and then in the directories defined by the variable include_path (set in the php.ini file).
•E.g. include_path has been given the value /home/apache/inc in the php.ini file, and the following
function call is executed:
fopen("data.txt", "r", 1);
If this call now fails to find the file data.txt in the
current directory, it will search in the directory
/home/apache/inc.
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 7
$fname=“a.txt”;
$fp=fopen($fname,”w”);
if(!$fp)
die(“can not open file”);
fclose($fp);
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 8
Closing a File
fclose() function:-
Once you've finished working with a file, it needs to be closed.
You can do this using fclose(), specifying the open file
by using its associated file handle as a single argument, fclose ($fp)
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 9
Read file()
1) fgetc() –
2) fgets() –
3) fgetcsv() –
4)fread()
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 10
Reading and writing characters in file
fgetc() - The fgetc() function can be used to read from files one character
at a time.
fgetc() takes a single argument, a file handle fp, and returns just one
character from the file it points to;
it returns False when it reaches the end of the file. This is basically the
same as the fread():
$one_char = fgetc($fp)
is equivalent to,
$one_char = fread($fp,1)
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 11
fgets() – This function reads string from file.
The difference between fgets() and fread() is that fgets() stops reading
when it reaches end-of-line and reads up to length -1 bytes, while fread()
reads past end-of-line and reads up to length bytes.
fgetcsv() – This function forms an array from CSV file.
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 12
4)fscanf()
Fscanf($fp,”%d %s” ,$rno, $name);
<?php
$fp=fopen("a.txt","r");
if(!$fp)
die ("Cannot open the file");
Fscanf($fp,"%d %s" ,$rno, $name);
echo $rno;
?>
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 13
5) Reading with fread()-
The fread() function can be used to extract a character string from a file.
Fread($fp,no. of bytes)
It takes two arguments: a file handle fp and an integer length. The function reads up to length
bytes from the file referenced by fp and return them as string.
$fp = fopen("data.txt", "r") ;$data = fread($fp, 10);
fread() will read the first 10 bytes from data.txt and assign them to $data as characters in a
string.
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 14
Writing to file
1)fputs()
2)fwrite()
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 15
Writing with fwrite()-
fwrite($fp, String to write,[length of characters to write]);
It requires two arguments: a file handle ($fp) and a string (string of text to write to the file), and
writes the contents of string to the file referenced by fp, returning the number of bytes written
(or -1 on error).
$fp = fopen("data.txt","w");fwrite($fp, "ABCxyz");
This writes the character string "ABCxyz" to the beginning of the file data.txt.
If you specify an integer length as a third argument, it stops writing after length bytes (assuming
this is reached before the end of string). For example:
fwrite($fp, "abcdefghij", 4);
writes the first 4 bytes of "abcdefghij" (that is, "abcd") to the file referenced by $fp.
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 16
Splitting name and path from file:
method 1
$f=basename($path)
Function takes complete path of file & returns just file name.
<?php
$path=“/var/www/html/a.txt”;
$f=basename($path);
echo $f;
?>
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 17
Splitting name and path from file:
method 2
$path=pathinfo(“/var/www/html/a.txt”);
echo $path[‘basename’];
echo $path[‘dirname’];
echo $path[‘extension’];
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 18
Rename and delete files
Rename-
Bool rename(old name,new name, [resource context])
Ex. Rename(“a.txt”,”b.txt”);
Copy-
Bool copy( source, destination)
Ex. copy(“a.txt”,”b.txt”);
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 19
Delete file
Unlink(file name)
To delete or foreget file
Make sure contents are not open before delete
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 20
5.4 Reading entire file
1)File()
file() – The file() function takes just one argument: a string containing
the name of a file.
Returns the entire contents of file as an array, using the newline
character (CR LF on the Windows platform) to delimit elements.
The newline character remains attached at the end of each line stored
in the array.
This function doesn't require you to specify a file handle because you
can refer to the filename explicitly;
it automatically opens, reads, and, once it's done, closes the file.
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 21
Array file(file name, [int use_include_path, resource context])
Ex.
$data=file(“a.txt”);
Print_r($data);
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 22
2)fpassthrough()
Reads data from current position till EOF
Write to buffer
Int fpassthrough(resorce handler)
Ex.
$fp=fopen(“a.txt”,”r”);
fgets($fp); //1st line
echo fpassthrough($fp); //from 2nd line till EOF
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 23
3)readfile()
Readfile(filename,[include_path,context])
Ex.
echo readfile(“a.txt”);
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 24

Chap 5 php files part 1

  • 1.
  • 2.
    What we learn? 5.1Working with files and directories 5.2 Opening and Closing, Getting information about file, Read/write to file, Splitting name and path from file, Rename and delete files 5.3 Reading and writing characters in file 5.4 Reading entire file MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 2
  • 3.
    1)Introduction:- Working with filesand directories 2 types of file- 1. Text file 2. Binary file 3. 3 steps- 1)Open file 2)read/ write file(operation) 3) Close file MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 3
  • 4.
    5.2 Opening andClosing 1)opening file- Fopen() 2)closing file- fclose() MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 4
  • 5.
    Fopen() •The fopen() isused to open a file, returning a file handle associated with the opened file. •It can take two or three arguments: filename, mode, and the optional use_include_path. •The file handle also can be used it to detect whether the file opened or not, if it is opened then file handle is a positive integer, if not, the file handle is zero. 1. $fp = fopen(“./data.txt", "r"); if(!$fp) die ("Cannot open the file"); ./data.txt points to the data.txt in the same directory. We can pass relative or absolute path. 2.if(!($fp = fopen("http://www.example.com/index.html", "r"))) die ("Cannot open the file."); MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 5
  • 6.
    Second parameter ismode r Open, file for reading only. Sets the file pointer at the beginning of file. r+ Open file for reading and writing. w file or writing only. If file is exist then content will be lost. Otherwise new file is created. w+ Open file for reading and writing. a Open file for appending only. If file is exist then contents will be appended to end of file, if file is not exist then new file is created. a+ Open file for reading and appending. MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 6
  • 7.
    Third (optional) parameteris use_include_path. •If use_include_path is set to 1, and the filename isn't specified as a relative or absolute path, the function searches for the file specified by filename first in current directory, •and then in the directories defined by the variable include_path (set in the php.ini file). •E.g. include_path has been given the value /home/apache/inc in the php.ini file, and the following function call is executed: fopen("data.txt", "r", 1); If this call now fails to find the file data.txt in the current directory, it will search in the directory /home/apache/inc. MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 7
  • 8.
    $fname=“a.txt”; $fp=fopen($fname,”w”); if(!$fp) die(“can not openfile”); fclose($fp); MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 8
  • 9.
    Closing a File fclose()function:- Once you've finished working with a file, it needs to be closed. You can do this using fclose(), specifying the open file by using its associated file handle as a single argument, fclose ($fp) MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 9
  • 10.
    Read file() 1) fgetc()– 2) fgets() – 3) fgetcsv() – 4)fread() MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 10
  • 11.
    Reading and writingcharacters in file fgetc() - The fgetc() function can be used to read from files one character at a time. fgetc() takes a single argument, a file handle fp, and returns just one character from the file it points to; it returns False when it reaches the end of the file. This is basically the same as the fread(): $one_char = fgetc($fp) is equivalent to, $one_char = fread($fp,1) MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 11
  • 12.
    fgets() – Thisfunction reads string from file. The difference between fgets() and fread() is that fgets() stops reading when it reaches end-of-line and reads up to length -1 bytes, while fread() reads past end-of-line and reads up to length bytes. fgetcsv() – This function forms an array from CSV file. MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 12
  • 13.
    4)fscanf() Fscanf($fp,”%d %s” ,$rno,$name); <?php $fp=fopen("a.txt","r"); if(!$fp) die ("Cannot open the file"); Fscanf($fp,"%d %s" ,$rno, $name); echo $rno; ?> MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 13
  • 14.
    5) Reading withfread()- The fread() function can be used to extract a character string from a file. Fread($fp,no. of bytes) It takes two arguments: a file handle fp and an integer length. The function reads up to length bytes from the file referenced by fp and return them as string. $fp = fopen("data.txt", "r") ;$data = fread($fp, 10); fread() will read the first 10 bytes from data.txt and assign them to $data as characters in a string. MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 14
  • 15.
    Writing to file 1)fputs() 2)fwrite() MONICADESHMANE(H.V.DESAI COLLEGE,PUNE) 15
  • 16.
    Writing with fwrite()- fwrite($fp,String to write,[length of characters to write]); It requires two arguments: a file handle ($fp) and a string (string of text to write to the file), and writes the contents of string to the file referenced by fp, returning the number of bytes written (or -1 on error). $fp = fopen("data.txt","w");fwrite($fp, "ABCxyz"); This writes the character string "ABCxyz" to the beginning of the file data.txt. If you specify an integer length as a third argument, it stops writing after length bytes (assuming this is reached before the end of string). For example: fwrite($fp, "abcdefghij", 4); writes the first 4 bytes of "abcdefghij" (that is, "abcd") to the file referenced by $fp. MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 16
  • 17.
    Splitting name andpath from file: method 1 $f=basename($path) Function takes complete path of file & returns just file name. <?php $path=“/var/www/html/a.txt”; $f=basename($path); echo $f; ?> MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 17
  • 18.
    Splitting name andpath from file: method 2 $path=pathinfo(“/var/www/html/a.txt”); echo $path[‘basename’]; echo $path[‘dirname’]; echo $path[‘extension’]; MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 18
  • 19.
    Rename and deletefiles Rename- Bool rename(old name,new name, [resource context]) Ex. Rename(“a.txt”,”b.txt”); Copy- Bool copy( source, destination) Ex. copy(“a.txt”,”b.txt”); MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 19
  • 20.
    Delete file Unlink(file name) Todelete or foreget file Make sure contents are not open before delete MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 20
  • 21.
    5.4 Reading entirefile 1)File() file() – The file() function takes just one argument: a string containing the name of a file. Returns the entire contents of file as an array, using the newline character (CR LF on the Windows platform) to delimit elements. The newline character remains attached at the end of each line stored in the array. This function doesn't require you to specify a file handle because you can refer to the filename explicitly; it automatically opens, reads, and, once it's done, closes the file. MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 21
  • 22.
    Array file(file name,[int use_include_path, resource context]) Ex. $data=file(“a.txt”); Print_r($data); MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 22
  • 23.
    2)fpassthrough() Reads data fromcurrent position till EOF Write to buffer Int fpassthrough(resorce handler) Ex. $fp=fopen(“a.txt”,”r”); fgets($fp); //1st line echo fpassthrough($fp); //from 2nd line till EOF MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 23
  • 24.