File Reading and Writing Functions

Author Topic: File Reading and Writing Functions  (Read 2146 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 158
  • Karma: 0
    • We Helpline
File Reading and Writing Functions
« on: 12-24-2015, 00:40:20 »
This is a supremely useful set of functions, particularly for data sets too small or scattered to merit the use of a database. File reading is pretty safe unless you keep unencrypted passwords lying around, but file writing can be quite unsafe.

Remember that although the Web server (and client-side languages such as JavaScript) can only act on files located under the document root, PHP can access files at any location in the file system—including those above or entirely outside the Web server document root—as long as the file permissions and include_path are set correctly. For instance, if your Web server document root is located at /usr/local/apache/htdocs, Apache will be able to serve only files from this directory and its subdirectories, but PHP can open, read, and write to files in /usr/local, /home/php, /export/home/httpd, or any other directory that you make readable and includable by the PHP and/or Web server user.

A file manipulation session might involve the following steps:

1. Open the file for read/write.
2. Read in the file.
3. Close the file (may happen later).
4. Perform operations on the file contents.
5. Write results out

Each step has a corresponding PHP filesystem function.

This archetypal example illustrates some subtleties of the syntax for manipulating file contents:

$fd = fopen($filename, “r+”)
or die(“Can’t open file $filename”);
$fstring = fread($fd, filesize($filename));
$fout = fwrite($fd, $fstring);


Related Topics

  Subject / Started by Replies Last post
3 Replies
Last post 05-29-2010, 17:35:35
by Marth22
3 Replies
Last post 12-19-2012, 00:48:36
by Swapnilramani
6 Replies
Last post 10-21-2013, 01:44:03
by emedianetwork
1 Replies
Last post 10-11-2013, 00:56:25
by emedianetwork
20 Replies
Last post 03-30-2016, 08:46:47
by sonth321