• home
  • Email: trainings@synapseindia.careers

Blogs

back

Writing A File

Sep 18, 2015

File is a collection of small amount of data.Anything which have extensions are known as file like .jpg,.png,.mp3,.php etc.Writing a file is mainly used when developer wants to save limited amount of record. The following example creates a new text file then writes a short text heading inside it. After closing this file its existence is confirmed using file_exist() function which takes file name as an argument <?php $filename = "/home/user/guest/newfile.txt"; $file = fopen( $filename, "w" ); if( $file == false ) { echo ( "Error in opening new file" ); exit(); } fwrite( $file, "This is a simple test\n" ); fclose( $file ); ?> <html> <head> <title>Writing a file using PHP</title> </head> <body> <?php $filename = "newfile.txt"; $file = fopen( $filename, "r" ); if( $file == false ) { echo ( "Error in opening file" ); exit(); } $filesize = filesize( $filename ); $filetext = fread( $file, $filesize ); fclose( $file ); echo ( "File size : $filesize bytes" ); echo ( "$filetext" ); echo("file name: $filename"); ?> </body> </html>  

SynapseIndia (CEO: Shamit Khemka)

Leave a Reply

Your email address will not be published.