- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP file_get_contents(): Read file into a string
The PHP file_get_contents() function is used when we need to read a file into a string. For example:
<?php $mystr = file_get_contents("codescracker.txt"); echo $mystr; ?>
The output of the above PHP example using the file_get_contents() function is:
Following is a snapshot of the opened file codescracker.txt, along with the current directory:
PHP Read File into String Preserving Newline or LineBreak
As you can see in the snapshot of the file codescracker.txt and the output shown above, the file contains text spread over three lines. But the output produced by the previous example, after getting the content of the file into a string, ignored all newline characters. Therefore, nl2br() comes into play. Here is an example:
<?php $mystr = file_get_contents("codescracker.txt"); $mystr = nl2br($mystr); echo $mystr; ?>
Or
<?php $mystr = file_get_contents("codescracker.txt"); echo nl2br($mystr); ?>
Both the examples above will produce the same output, which should be:
PHP file_get_contents() Syntax
The syntax of the file_get_contents() function in PHP is:
file_get_contents(path, include_path, context, start, length)
The first parameter (path) is required, whereas all three other parameters are optional.
Note: The include_path parameter is used when we need to search the file in the include_path (in php.ini). To do this, we need to specify this parameter as 1.
Note: The context parameter is used to specify the context in which to handle the file.
Note: The start parameter is used when we need to read a file into a string from any particular position. Negative values will be used to read from the specified position, from the end.
Note: The length parameter is used when we need to read a file into a string up to the specified maximum length of characters.
Advantages of the file_get_contents() function in PHP
- It only takes one line of code to use this function to read a file's contents into a string.
- This function reads the entire contents of the file into memory at once rather than line by line, making it very efficient.
- This function can be used to read the content of files that are accessible via a URL or stored on distant servers.
- If there are problems accessing the file or URL, this function provides thorough error messages.
Disadvantages of the file_get_contents() function in PHP
- This function reads the entire file's contents into memory, which can be problematic for large files or systems with limited memory.
- If this function is used to read files controlled by the user or to access potentially malicious URLs, it can pose a security risk.
- This function may cause performance issues if used to read large files or if the network connection fails when accessing remote files.
- Although this function generates detailed error messages, it is not always clear what action should be taken to resolve the issue.
« Previous Tutorial Next Tutorial »