File handling in php
This chapter will explain following functions related to files:
Opening and Closing FilesThe PHP fopen() function is used to open a file. It requires two arguments stating first the file name and then mode in which to operate.Files modes can be specified as one of the six options in this table.
After making a changes to the opened file it is important to close it with the fclose() function. The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails. Reading a fileOnce a file is opened using fopen() function it can be read with a function called fread(). This function requires two arguments. These must be the file pointer and the length of the file expressed in bytes.The files's length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes. So here are the steps required to read a file with PHP.
Writing a fileA new file can be written or text can be appended to an existing file using the PHP fwrite() function. This function requires two arguments specifying a file pointer and the string of data that is to be written. Optionally a third integer argument can be included to specify the length of the data to write. If the third argument is included, writing would will stop after the specified length has been reached.The following example creates a new text file then writes a short text heading insite it. After closing this file its existence is confirmed using file_exist() function which takes file name as an argument
|
Monday, 5 August 2013
PHP Files & I/O
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment