-------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD-IT)
Contact No : +91 7600044051
E-Mail : rupareleducation@gmail.com
PHP File Handling function
No
|
Name
|
Description
|
1
|
fopen()
|
It is used to
open the file or URL.
If php has
decided that file name specifies a local file, that it will try to open a
that file. The file must be accessible to php. so that the file access that
the file access permission allow this access.
The mode
parameter specifies the type of mode, you require to the file.
Syntax
fopen(filename,mode)
following is list
of possible modes for fopen
MODE Description
‘r’ Open for
reading only place a file pointer at the beginning
of the file.
‘r+’ Open for
reading & writing, place the file pointer at the
beginning of the file.
‘w’ Open for
writing, place the file pointer at the beginning
of the file.if the file does not exists attempt to
creating it.
‘w+’ Open for reading
& writing,
‘a’ Open for
writing only, place the file pointer at the end of the
file. it does not exists attempt to create it.
‘a+’ Open for
reading & writing, place the file pointer at the EOF.
‘x’ Create and
open for writing only, place the file pointer at
the beginning of the file. If the file already exists the
fopen call will fail by returning false & generates
error. This option is supported in PHP 4.3.2
& later & only works for local files.
‘x+’ Create & open for reading &
writing.
|
2
|
fread()
|
It is used to
read up to the length bytes from the file pointer. Reading stops when length
bytes have been read.
Syntax:
fread(length)
Example:
<?php
$filename=”xyz.txt”;
$handle=fopen(“$filename”,”r”);
$contents=fread($handle,filesize($filename));
echo $contents;
fclose($handle);
?>
|
3
|
fwrite()
|
It is used to
write the content of string to the file. If the length argument is given
writing will stop after length bytes have been written for the end of the
string is reached.
Syntax
fwrite(string,int
length)
Example
<?php
$filename=”xyz.txt”;
$newline=”Add this text to the
file.”;
$handle=fopen($filename,’a’);
fwrite($handle,$newline);
fclose($handle);
?>
|
4
|
fclose()
|
It is used to
close an open file.
Syntax
fclose(filename)
It returns true
on success or false on failure.
Example
<?php
$handle=fopen(‘somefile.txt’,’r’);
fclose($handle);
?>
|
5
|
File_exists()
|
It checks
whether a file or directory exists or not, it returns true if the file exists
which specified by file name, otherwise false.
Syntax
file_exists(string
filename)
Ex:
<?php
$filename=”xyz.txt”;
If(file_exists($filename))
{
echo “The file $filename
exists”;
}
else
{
echo “The file $filename does not
exists”;
}
?>
|
6
|
Is_readable()
|
It is used to
tell whether the file is readable. It returns true, if filename exists and is
readable.
Syntax
is_readable(filename);
Example:
<?php
$filename=’xyz.txt’;
If(is_readable($filename))
{
echo “The file is
readable”;
}
else
{
echo “The file is not
readable”;
}
?>
|
7
|
is_writable()
|
It is used to
tell whether the file name is writable or not. It returns true, if the
filename exists and is writable.
Syntax
is_writable(filename);
Example
<?php
$filename=’xyz.txt’;
If(is_writable($filename))
{
echo “The file is
writable”;
}
else
{
echo “The file is not
writable”;
}
?>
|
8
|
fgets()
|
It is used to
gets line from file pointer. It returns a string up to length.
Syntax
fgets(filename);
Example
<?php
$file = fopen("test.txt","r"); echo fgets($file); fclose($file); ?> |
9
|
Fgetc()
|
It is used to
gets a character from the given file pointer. The file pointer must be valid
& must be file successfully opened for fopen.
Syntax
fgetc(filename);
Example
<?php
$file = fopen("test2.txt","r"); echo fgetc($file); fclose($file); ?> |
10
|
file()
|
It is used to
reads entire file into an array. It is identical to read the file &
returns the file in array.
Syntax
file(filename);
Example
<?php
print_r(file("test.txt")); ?> |
11
|
File_get_contents ()
|
It is used to
reads entire file, into a string. It is preferred way to read a content of a
file into the string.
Syntax
file_get_contents
(filename);
Example
<?php
$str=file_get_contents (“text.txt”);
Echo
$str;
?>
|
12
|
File_put_contents()
|
This function is
identical to calling fopen,fwrite & fclose successively to write data to
a file.
If file does not
exists, the file is created. Otherwise the existing file is overwritten.
Syntax
file_put_contents(filename);
Example
<?php
echo file_put_contents("test.txt","Hello
World. Testing!");
?> |
13
|
ftell()
|
It returns the
position of the file pointer reference by handle. The file pointer must be
valid & successfully open by fopen.
Syntax
ftell(filename);
Example
<?php
$file = fopen("test.txt","r"); echo ftell($file); fseek($file,"15"); echo "<br />" . ftell($file); fclose($file); ?> |
14
|
fseek()
|
It is used to
sets the file position indicator for the file. The new position measured in
bites from the beginning of the file.
Syntax
fseek(filename);
Example
<?php
$file = fopen("test.txt","r"); // read first line fgets($file); // move back to beginning of file fseek($file,0); ?> |
15
|
copy()
|
It is used to
copies a file.
Syntax
copy(source
file,destination file);
Example
<?php
$file=’xyz.txt’;
$newfile=”d:/abc.txt”;
copy($file,$newfile);
echo “copy the file successfully”;
?>
|
16
|
unlink()
|
It is used to
delete a file.
Syntax
unlink(filename);
<?php
$file = "test.txt"; if (!unlink($file)) { echo ("Error deleting $file"); } else { echo ("Deleted $file"); } ?> |
17
|
rename()
|
It is used to
rename a file or directory.
Syntax
rename(old name,
new name)
Example
<?php
rename(“d:/xyz.txt”,”d:/mnk.txt”);
echo “renaming the file”;
?>
|
18
|
move_upload_file()
|
This Function
check to ensure that the file designated by filename is valid upload file. If
the file is valid, it will be moved to the filename given by destination.
<?php if (move_uploaded_file($_FILES['file']['tmp_name'], "/images/")) { echo "Uploaded successfully!"; } else { echo "Upload failed!"; } ?> |
Please share and comment it... Thank you.
https://myshopprime.com/uday.shah3/shop
:: Best of Luck ::
https://myshopprime.com/uday.shah3/shop