PHP File handling

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )

Parameters

ParameterDescription
filenameThe filename being read.
use_include_pathYou can use the optional second parameter and set it to TRUE, if you want to search for the file in the include_path, too.
contextA context stream resource.

Remarks

Filename syntax

Most filenames passed to functions in this topic are:

  1. Strings in nature.
    • File names can be passed directly. If values of other types are passed, they are cast to string. This is especially useful with SplFileInfo, which is the value in the iteration of DirectoryIterator.
  2. Relative or absolute.
    • They may be absolute. On Unix-like systems, absolute paths start with /, e.g. /home/user/file.txt, while on Windows, absolute paths start with the drive, e.g. C:/Users/user/file.txt
    • They may also be relative, which is dependent on the value of getcwd and subject to change by chdir.
  3. Accept protocols.
    • They may begin with scheme:// to specify the protocol wrapper to manage with. For example, file_get_contents("http://example.com") retrieves content from http://example.com.
  4. Slash-compatible.
    • While the DIRECTORY_SEPARATOR on Windows is a backslash, and the system returns backslashes for paths by default, the developer can still use / as the directory separator. Therefore, for compatibility, developers can use / as directory separators on all systems, but be aware that the values returned by the functions (e.g. realpath) may contain backslashes.


Got any PHP Question?