Php File-directory Examples Getting path components basename.php Using the chown() and chgrp() Functions Changing File Permissions and Ownership Changing file permissions Using the chmod() Function chmod() function changes the mode of filename to permissions. Changing file owner and group Copying Files with copy( ) Copying a file Copying files Make a copy of a fileUsing the copy() Function Generating comma-separated data Making a CSV-formatted string Parsing CSV Strings Read csv file Reading CSV data from a file Tell the web client to view the CSV file in a seprate program fgetcsv.php Printing comma-separated data Putting comma-separated data into a string Creating and Destroying a Directory Handle Deleting a Directory by Using rmdir() Delete an empty directory called dir1 Listing the Contents of a Directory with readdir() Calculate the size for a directory readdirscandirThe current working directory Print all files under a directory Read the content from a directory Reading Contents from a Directory Clean Path by Regular Expressions dirname.php Use dirname() with predefined variable $SCRIPT_FILENAME to obtain the complete path of the script executing the command Disk total space Get disk free space for Unix/Linux Get free disk space for Cdisk_free_space.php disk_total_space.php Close a file you have opened with fopen( ), use fclose( ). fclose() function closes the file Counting lines in a file Reading lines from a file feof.php Read till the end of a file Flushing output fgetc() function returns one character from the file or returns false on reaching the end of file. Reading Characters with fgetc() Checking for an error from fopen(), fgets(), or fclose() Counting paragraphs in a file Counting records in a file Creating and Using a File Class Reading a compressed file Reading a file a line at a time fgets() function returns a string read from a file. fgets.php Processing a list of books Processing each word in a file fgetss() strips all HTML and PHP tags from the filefgetss.php Stripping all but a select few tags from an HTML file Close a file after reading Rename() function for moving files Use of the copy() function Delete a file File existance Reading a File into an Array file.php PHP File Logic Functions Integer Arguments to the flock() Function Checking That a File Exists Is it a fileIs the file a directoryIs the file readableIs the file writableFile changed time File modified time File accessed time Checking the Mode of a File Determining the Size of a File Get the file sizeIs the file executablefilegroupFile last changed time File last accessed time Get the file owner Get file permission File last updated time Reading a Specific Character Using fgets() and feof() Functions Reading Specific Data from a File Reading from a File Opening and Reading a File Line by Line Reading a File with fread() File read by char Moving Around a File with fseek() Save Upload File to a new directory Upload PDF file and rename it HTML form with a file input tag A File UploadFile Upload Global Variables Writing and Appending Content in a File Write content to file Writing and Appending to a File dirnamerealpathFile base name Getting Date Information About a File fileatime.php Get the change time of a document with the filectime() function. filectime.php filegroup.php Getting file timestamps filemtime.php discover the modification date of a file with the function filemtime() fileowner.php To read the owner of a file, use the fileowner( ) function fileperms.php Determining File Size with filesize() filesize() function returns the size, in bytes. filesize.php Checking Whether a File Exists Checking for Existence with file_exists() Checking the existence of a file file_exists() function returns true if it does, and false otherwise. The file_exists.php script checks to see whether the file is there Check net speed by loading a file Checking for an error from file_get_contents() Fetching a URL with file_get_contents() Reading a file into a string file_get_contents.php file_put_contents( ) writes to a file with the equivalent of fopen( ), fwrite( ), and fclose( ) all in one function, just like file_get_contents( ). Use file_get_contents with a URL Retrieving a protected page Retrieving a remote page with file_get_contents() Changing a file in place Checking for an error from file_put_contents() Saving a file with file_put_contents() Locking Files with flock( ) If file lock is not available, flock( ) will return immediately with false rather than wait for a lock to become available. Using advisory file locking The file locking mechanism in PHP makes processes queue up for their locks by default Acceptable fopen() Modes Calling fopen() with a Context Resource Opening a file Opening a file on Windows Opening a remote file Opening files in the include_path Using fopen( ) Using the fopen() Function File open with exception checking Getting and Printing a Web Page with fopen() Getting and Putting Files with FTP Reading from standard input If you are writing a binary file on a Windows system, you should add a 'b' flag. Load remote file Open a connection with the PHP site (httpOpening Files fopen() function opens a file, returns an integer, as a file handle. fopen() requires the file path and the mode in which the file is to be opened. fopen() returns false if the file cannot be opened for any reason. PHP 5 Arguments for Opening a File Safely reading a binary file A Simple Text-File Hit Counter Using the fputs() Function Storing data on a remote server Reading a file fread( ) is good for when you only care about a small part of the file. fread() function reads up to length bytes from the file, returning the file's contents. fread.php fopen( ) and fread( ) Reading and Writing Binary Data in a File Acceptable Format Values for fscanf() fscanf.php Reading Formatted Text Using fscanf() Constant Reference Points for fseek() Using the fseek() Function Outputting the Status Lines Returned by Web Servers fsockopen() function establishes a socket connection, TCP or UDP. fsockopen.php SSL Streams Sockets Are Files Scan a server Retrieving a Web Page Using fsockopen() A simple access counter Appending to a file fwrite() function writes string to the file. fwrite.php Using file-related error information Writing data to a file Storing user information in a text file Reading and Changing the Working Directory getlastmod.php getlastmod() function returns the most recent modification date and time of the page getlastmod() returns to you a Unix timestamp with the last modified date. Getting Times and Dates of Files Output the last time your script was updated Using the glob() Function Using glob() glob() Constants is_dir() function verifies that the file is a directory Creating a Function That Performs Multiple File Tests Is a directory Working with Directories is_executable is_executable() tells you whether you can run a file A File or a Directory? is_file() function returns true if file exists and is a readable/writable file. Using the File-System Logic Functions is_readable is_readable() function checks the readability of both a file and a directory. is_readable() tells you whether you can read a file. is_writeable is_writable() tells you whether you can write to a file. Testing for write permission Creating Directories with mkdir() Making a directory Using the move_uploaded_file() Function Listing directory Reading a Directory Listing Using opendir() pathinfoGetting path components and file extensions pathinfo.php Using popen() and pclose() Using popen() to Pass Data to the column Application Using popen() to Read the Output of the Unix who Command Using popen() with nsupdate Reading output from popen() Opening a Process File Pointer with popen() Opening a Unidirectional Pipe Using popen() Redirecting standard output Reading standard error readdir.php Procedural directory iteration readfile() function reads in a file and outputs it to standard output. readfile.php readlink( ) function takes a link name as its only parameter and returns the target that the link points to. Cleaning up a file name with realpath() realpath.php A file can be renamed with the rename() function Moving Files with rename( ) Moving a file rewind and fseek Removing a Directory with rmdir() Listing All Files in the Current Directory scandir.php The scandir( ) function takes a minimum of one parameter with an optional second. Converting file permission values stat() function returns Unix timestamps for the date and time data stat() function returns a comprehensive indexed array of file information File accessed, modified, created time stream_context_create stream_get_filters stream_filter_append Reading compressed data from a URL Making a symbolic link symlink.php Working with Links Generating a filename with tempnam() Creating a temporary file in a particular place Creating a temporary file with tempnam() Creating a temporary file with tmpfile() Read text file into array and output Read text file with format Read file content with only one command Display XML file Read in and perform operations on a file line by line Read text file into an array in one function Try to open a text file Viewing the Source of a Document Write string to text file How to add text to the file Display contents of txt file to browser Changing file modification times Error-prone file locking If a file does not yet exist, you can create one with the touch() function. Deleting Files with unlink( ) Delete a file with the unlink() function. Using file_exists, touch, and unlink together Using the unlink() Function You can remove an existing file with the unlink() function. Deleting a file Deleting files Moving a file across filesystems bool virtual ( string filename ) performs a virtual request to the local Apache web server for a file $_FILE Error Constants Constant $_FILES Arguments Echoing the line and file predefined constants Using __FILE__ and __LINE__

Php Code : Readfile.php

readfile.php in php

 
<?php
   $file = "data.txt";

   $tags = "<h2><h3><p><b><a><img>";

   $fh = fopen($file, "rt");

   while (!feof($fh))
      $article .= fgetss($fh, 1024, $tags);

   fclose($fh);

   $fh = fopen($file, "wt");
   fwrite($fh, $article);
   fclose($fh);

   $bytes = readfile($file);
?>