ANSWERS: 1
  • Reading files in PHP is a common task with hundreds of potential applications such as reading web application configuration files, processing web site templates, copying files, ingesting RSS feeds and using files in lieu of a database system for web site data and content. You can easily open and read files stored on the server or available on the Internet using PHP.

    Open and Close

    Open a file using the fopen function. This function takes two arguments: the path and file name and the mode. The mode is a predefined string indicating the type of access, such as read-only ("r"), read-write ("r+"), or append ("a"). The function returns a file handle used for reading from the file. Using the read-only mode, the file cursor will point to the beginning of the file when it is opened. $fh = fopen("filename.ext", "r"); fopen can also be used to open a URL, for example: $fh = fopen("http://website.com/", "r"); $fh = fopen("ftp://ftp.somesite.edu/file.txt", "r"); Close the file with the fclose function: fclose($fh);

    Basic Read Functions

    fgets reads one line of data at a time, leaving the file cursor pointing at the next line in the file: $line = fgets($fh); fread reads a specified number of bytes from a file, which is more appropriate for binary files than fgets. Like fgets, the file cursor is left pointing at the next byte past the end of the read-in data. This example reads 1024 bytes from the file. $data = fread($fh, 1024);

    Read into Variables

    The command fscanf can populate a list of variables as it reads from a file, automatically parsing values based on the provided formatting string. This function is useful for reading name-value pairs from configuration files, delimited data, and other files containing structured information. For example, this line reads two strings and an integer from a tab delimited file: list ($firstname, $lastname, $age) = fscanf($fh, "%s\t%s\t%d\n"); Refer to the "sprintf" page for a list of format specifiers.

    Read Text from HTML and PHP

    The command fgetss is similar to fgets, but it strips HTML and PHP from the input as it reads from a file. This is great for scraping web pages and converting HTML to plain text. For example: HTML input: <p><strong>fgetss:</strong> filters out HTML and PHP from file input.</p> PHP fgetss: echo = fgetss($fh); Results in the following output: fgetss: filters out HTML and PHP from file input.

    Looping

    The feof function checks to see if the file cursor is at the end of the file. This function can be used to control a loop for reading through an entire file line-by-line or in chunks. For example, this while loop echos the entire file to output, stopping when the end of the file is reached: while (!feof($fh)) { echo = fgets($fh); }

    Random Access

    The fseek function provides functionality for jumping back and forth through a file by a specified number of bytes. For example, go to the start of the file: fseek($fh, 0); Jump to four kilobytes from the beginning of the file: fseek($fh, 4096); Move the file cursor 512 bytes forward then 120 bytes backward from the current position: fseek($fh, 512, SEEK_CUR); fseek($fh, -120, SEEK_CUR); Move the file cursor 24 bytes from the end of the file: fseek($fh, -24, SEEK_END);

    Source:

    PHP Manual

Copyright 2023, Wired Ivy, LLC

Answerbag | Terms of Service | Privacy Policy