File System or File Handling in php
One of the most useful features of a server side language is its ability to interface with the file system on the server that hosts the website.
File system is a linear approach.
In php we create a web application without using rdbms(relational database management system ) by using file system.
In file system or file handling we working with diffrent types files and directories or folders.
WORKING WITH FILES-
HOW TO CREATE A FILE-
To create a file in php we use function touch()syntax :
touch($filename);and another method to create a file is
syntax :
fopen($filename,"w");
Ex-
<?phptouch("test.pdf");
?>
output :
its create a pdf file of name test in current directory.HOW TO DELETE A FILE-
To delete a file in php we use function unlink()syntax :
unlink($filename);Ex-
<?phpunlink("test.pdf");
?>
output :
its delete a pdf file of name test in current directory.HOW TO ACCESS FILES-
To access a file you first have to establish a connection to that file. This is done by opening the file with the fopen function.To use the fopen function you provide it two parameters.
The first is the name of the file you wish to use
and the second says how you want to use the file (for reading or writing, etc.)
Using the fopen function establishes a pointer to the file. Here's an example:
<?php$myfilepointer = fopen("myfilename.txt", "w");
?>
here, we're opening a file called "myfilename.txt" and saying that we wish to write to it. The "w", which is what tells PHP we want to write to the file, is called the Access Mode. Other values that can be used, and their meanings are:
r read only
r+ reading and writing
w write only & create the file if it doesn't exist
w+ reading and writing & create the file if it doesn't exist
a writing only, create if it doesn't exist, and place a file position pointer at the end of the file (appends records to an existing file)
a+ reading and writing, create if it doesn't exist and place file position pointer at the end of the file
HOW TO WRITE A FILE :
When writing to a file, the first thing you need to do is to open up the file. We do that with this code:<?php
$File = "YourFile.txt";
$Handle = fopen($File, 'w');
?>
Now we can use the fwrite command to add data to our file. We would do this as shown below:
<?php
$File = "YourFile.txt";
$Handle = fopen($File, 'w');
$Data = "Jane Doe\n";
fwrite($Handle, $Data);
$Data = "Bilbo Jones\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
?>
ANOTER WAY TO WRITE A FILEEX-
<?php$file = 'REX.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
file_put_contents($file, $person, FILE_APPEND );
?>
HOW TO READ A FILE :
USE OF fread()
Definition: Fread () reads data from an external file and reports it back to your PHP file. It read the number of bytes that you specify. It is phrased as: fread ( handle, length ). You must open the file in the handle. The length is measured in bytes, with a 8192 byte maximum.
Also Known As: file read, php file readExamples:
<?php$myFile = "MyFileName.txt";
$handle = fopen($myFile, 'r');
$Data = fread($handle, 10);
fclose($handle);
echo $Data;
?>
USE OF fgets()
Definition: The PHP function fgets () is used to read a file line by line. This will only work if the file data is stored on separate new lines, if it is not this will not produce the results you are looking for. It is phrased as: fgets (handle, length). The File must be opened in the handle. Length does not need to be included, but it will default to 1024 bytes if it is not specified.
Examples:
<?php$myFile = "YourFile.txt";
$handle = fopen($myFile, 'r');
while (!feof($handle))
{
$data = fgets($handle, 512);
echo $data;
echo "<br>";
}
fclose($handle);
?>
USE OF file_get_content()
Definition: File_Get_Contents () works the same as file () except it returns a string instead of an array. This function retrieves data from an external file and then puts it into a string for use.
Examples:
<?php
$file = file_get_contents ('rex.txt');
Echo $file;
?>
HOW TO RENAME A FILE :
USE OF rename()
Definition: rename () is used to rename a file:
Syntax :
rename($currentFileName,$newFileName);
Ex-
<?php
//which rename the text file of name rex into text file of name test
rename("rex.txt","test.txt");
?>
HOW TO COPY A FILE :
USE OF copy()
Definition: rename () is used to copy a file from destination to source address:
Syntax :
copy("destination/$FileName","target/$FileName);
Ex-
<?php
//which copy the text file of name rex into folder new which placed in current directory.
rename("rex.txt","new/rex.txt");
?>
WORKING WITH DIRECTORIES
PHP directories functions are provided to manipulate any directory.
List of Functions
Function chdir()
Definition and Usage
Changes PHP's current directory to the passed directory.
Example
Following is the usage of this function:
<?php
// current directory
echo getcwd() . "\n";
chdir('html');
// current directory
echo getcwd() . "\n";
?>
This will produce following result:
/home/tutorialspoint
/home/tutorialspoint/html
Function chroot()
Definition and Usage
Changes the root directory of the current process to the passed directory.
<?php
chroot('/var/www');
?>
Function dir()
Definition and Usage
The dir() function opens a directory handle and returns an object. The object contains three methods called read() , rewind() , and close() .
<?php
$d = dir("/var/www/");
while (false !== ($entry = $d->read())) {
echo $entry."\n";
}
$d->close();
?>
Function opendir() AND readdir()
Definition and Usage
Opens up a directory handle to be used in subsequent closedir(), readdir(), and rewinddir() calls.Example
Following is the usage of this function:<?php
//Open images directory
$dir = opendir("/var/www/images");
//List files in images directory
while (($file = readdir($dir)) !== false)
{
echo "filename: " . $file . "<br />";
}
closedir($dir);
?>
This will produce following result:
filename: .filename: ..
filename: logo.gif
filename: mohd.gif
Function scandir()
Definition and Usage
Returns an array of files and directories from the passed directory.<?php
$dir = 'new';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
print_r($files1);
//print_r($files2);
?>
No comments:
Post a Comment