Ø fopen()
- This function is used to open the file or the URL.
Syntax
resource fopen(string filename, string mode)
Parameter | Description |
Filename | Where the filename can be either entire path where the file is stored or just the filename. |
Mode | The mode is to be specified for opening the file. |
A list of possible modes for fopen() using mode | |
mode | Description |
'r' | Open for reading only; place the file pointer at the beginning of the file. |
'r+' | Open for reading and writing; place the file pointer at the beginning of the file. |
'w' | Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
'w+' | Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. |
'a' | Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
'a+' | Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
'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 and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. |
'x+' | Create and open for reading and writing; otherwise it has the same behavior as 'x'. |
Example
<?php
fopen("test.txt","r");
?>
Ø fread()
- This function is used for reading the contents from the file. It will read the file till the length is specified or till the end of file(EOF) is reached.
- To find the EOF filesize() function is used. The content retrieved are of string data type.
Syntax
String fread(file handler variable, int length)
Example
<?php
$handler = fopen("test.txt","r");
$content = fread($handler,filesize("test.txt"));
echo $content;
?>
Ø fwrite()
- Writes the content into the file specified. fwrite() returns the number of bytes written, or false on error.
Syntax
int fwrite(file handler variable, string $string [,int length)
Example
<?php
$handler = fopen("test.txt","a");
$content = fwrite($handler," Hello");
?>
Ø fclose()
- Close an open file pointer.
- Return true on success and false and failure.
- The file pointer must be valid, and must point to a file successfully opened by fopen().
Syntax
bool fclose(file handler variable)
Example
<?php
$handler = fopen("test.txt","r");
fclose($handler);
?>
Ø file_exists()
- Checks whether a file or directory exist.
- Returns true if the file or directory specified by filename exists, false otherwise.
Syntax
bool file_exists(string filename)
Example
<?php
if(file_exists("test.txt"))
{
echo "File is exists";
}
else
{
echo "File does not exists";
}
?>
Ø is_readable()
- Tells whether the filename is readable.
- Return true if the filename exists and is readable.
Syntax
bool is_readable(string filename)
Example
<?php
if(is_readable("test.txt"))
{
echo "File is readable";
}
else
{
echo "File is not readable";
}
?>
Ø is_writable()
- Tells whether the filename is readable.
- Return true if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.
Syntax
bool is_writable(string filename)
Example
<?php
if(is_writable("test.txt"))
{
echo "File is writable";
}
else
{
echo "File is not writable";
}
?>
Ø fgets()
- It reads the content of the file line by line. The return type is string.
- To get the end of the file the function feof() is to be used.
Syntax
String fgets(file handler variable [,integer length])
Example
<?php
$handler = fopen("test.txt","r");
$str = fgets($handler);
echo $str;
?>
Ø fgetc()
- Gets a character from the given file pointer.
Syntax
String fgetc(file handler variable)
Example
<?php
$handler = fopen("test.txt","r");
$str = fgetc($handler);
echo $str;
?>
Ø file()
- This function will read the entire file in the array format so the return type is the array.
- Each line in the file is stored as the new line in the array. There are no requirement to open the file, it gets automatically where the file() is used.
Syntax
array file(string fiename)
Example
<?php
$str = file("test.txt");
print_r($str);
echo $str[0];
?>
Ø file_get_contents()
- It read the content of the file and store it in the string format.
- There is no requirement to open the file, it gets automatically opened, also there is no need to give the length. It will read till the end of the file.
Syntax
String file_get_contents(string fiename)
Example
<?php
$str = file_get_contents("test.txt");
echo $str;
?>
Ø file_put_contents()
- It writes the content into the file. There is no requirement to open the file it gets automatically opened for writing.
Syntax
void file_put_contents(string filename, string data to be written)
Example
<?php
file_put_contents("test.txt"," World");
$str = file_get_contents("test.txt");
echo $str;
?>
Ø ftell()
- ftell() function is used to give the current position in the file. It return integer value.
Syntax
integer ftell(file handler variable)
Example
<?php
$handler = fopen("test.txt","r");
$str = fgetc($handler);
echo ftell($handler);
?>
Ø fseek()
- This function enables you to change your current position within a file, it requires the file resources and an integer that represent the offset from the start of the file which you want to jump.
Syntax
integer fseek(file handler variable, integer offset)
Example
<?php
$handler = fopen("test.txt","r");
$str = fgets($handler);
echo $str."<br/>";
echo ftell($handler)."<br/>";
fseek($handler,0);
echo ftell($handler)."<br/>";
?>
Ø rewind()
- set the file position indicator for the handler to the beginning of the file stream.
- The file pointer must be valid, and must point to a file successfully opened by fopen().
Syntax
bool rewind(file handler variable)
Example
<?php
$handler = fopen("test.txt","r");
$str = fgets($handler);
echo $str."<br/>";
echo ftell($handler)."<br/>";
rewind($handler);
echo ftell($handler)."<br/>";
?>
Ø copy()
- copies file
Syntax
bool copy(string source, string destination)
- makes a copy of the file source to destination.
- Return true on success or false on failure.
Example
<?php
copy("test.txt","test4.txt");
echo "File successfully copy";
?>
Ø unlink()
- Deletes a file
- Return true on success or false on failure.
Syntax
bool unlink(string filename)
Example
<?php
unlink("test4.txt");
echo "File successfully delete";
?>
Ø rename()
- Rename a files.
- Return true on success or false on failure.
Syntax
bool rename(string oldfilename, string newfilename)
Example
<?php
rename("test4.txt","text5.txt");
?>
Ø move_upload_file()
- This function will move an uploaded file into the new location. It returns true if the file is moved else will return false.
Syntax
bool move_upload_file(source filename, destination filename)
Example
<?php
if ($_REQUEST[completed] == 1)
{
$newname = uniqid("hemangi").".jpg";
move_uploaded_file($_FILES['mailfile']['tmp_name'],"images/$newname");
$_FILES['tem_name'];
}
?>
<html>
<head><title>Upload page</title></head>
<body><h1>Image Uploader</h1>
<?php if ($_REQUEST[completed] != 1) { ?>
<b>Please upload an image</b><br>
<form enctype="multipart/form-data" method="post">
<input type="hidden" name="completed" value="1">
Choose file to send: <input type="file" name="mailfile"> and <input type="submit">
</form>
<?php } else { ?>
<b>Image Upload</b>
<?php } ?>
</body>
</html>
Posted By : Hemangi Zala
No comments:
Post a Comment