eHow launches Android app: Get the best of eHow on the go.

How To

How to Use the File Function in PHP

Contributor
By eHow Contributing Writer
(0 Ratings)

The file function in PHP reads a file into an array and returns the array. You should use file_get_contents instead if you would like the contents of the file returned as a string. The following steps will help you use the file function in PHP.

Difficulty: Moderate
Instructions
  1. Step 1

    Learn the syntax. The complete syntax of file is: array file (string $filename [, int $flags [, resource $context]]).

  2. Step 2

    Use the $filename parameter to specify the file path. It can be a URL if you have enabled the fopen wrappers. The protocol/wrappers that the file function supports include FTP, FTPS, HTTP and HTTPS. You can also use the file function to establish PHP input/output streams, compression streams and audio streams.

  3. Step 3

    Specify the behavior of the file function with the optional $flag parameter. The FILE_BINARY constant will cause the file function to read the file content as binary data. FILE_IGNORE_NEW_LINES will prevent the file function from adding a newline character. FILE_SKIP_EMPTY_LINES will skip empty lines. FILE_TEXT will return the file content in UTF-8 encoding. FILE_USE_INCLUDE_PATH will cause the file function to look in the include path for the file.

  4. Step 4

    Refer to context resource with the $context parameter. You may create this resource with the stream_context_create function.

  5. Step 5

    Look at the following examples of the file function:
    $lines = file('http://www.test.com/');
    // read the web page test.com into an array.;
    $html = implode('', file('http://www.test.com/'));
    // read the web page test.com into a string;
    $trimmed = file('temp.txt', FILE_IGNORE_NEW_LINES | ;
    FILE_SKIP_EMPTY_LINES);
    // read the file temp.txt into an array but do not add any newline characters;
    // and skip empty lines.;

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics