File handling is an important part of any web application. You often need to open and process a file for different tasks.
PHP Manipulating Files
PHP has several functions for creating, reading, uploading, and editing files.
Be careful when manipulating files!
When you are manipulating files you must be very careful.You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.
PHP readfile() Function
The readfile() function reads a file and writes it to the output buffer.
Assume we have a text file called "webdictionary.txt", stored on the server, that looks like this:
AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
The PHP code to read the file and write it to the output buffer is as follows (the readfile() function returns the number of bytes read on success):
The readfile() function is useful if all you want to do is open up a file and read its contents.
The next chapters will teach you more about file handling.
PHP Exercises
Test Yourself With Exercises
Exercise:
Assume we have a file named "webdict.txt", write the correct syntax to open and read the file content.
Files are included based on the file path given or, if none is given, the specified. If the file isn't found in the , include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit an E_WARNING if it cannot find a file; this is different behavior from require, which will emit an E_ERROR.
Note that both include and require raise additional E_WARNINGs, if the file cannot be accessed, before raising the final E_WARNING or E_ERROR, respectively.
If a path is defined — whether absolute (starting with a drive letter or include0 on Windows, or include1 on Unix/Linux systems) or relative to the current directory (starting with include2 or include3) — the will be ignored altogether. For example, if a filename begins with include4, the parser will look in the parent directory to find the requested file.
For more information on how PHP handles including files and the include path, see the documentation for .
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
Example #1 Basic include example
include6
include7
include8
include9
include0
include1
include2
include8
If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function. An exception to this rule are magic constants which are evaluated by the parser before the include occurs.
Example #2 Including within functions
include4
include1
include6
include7
include8
include8
When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.
If "" are enabled in PHP, you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.
Example #3 include through HTTP
E_WARNING1
E_WARNING2
E_WARNING3
E_WARNING4
Warning
Remote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server. If the file from the remote server should be processed there and outputted only, readfile() is much better function to use. Otherwise, special care should be taken to secure the remote script to produce a valid and desired code.
See also Remote files, fopen() and file() for related information.
Handling Returns: include returns E_WARNING6 on failure and raises a warning. Successful includes, unless overridden by the included file, return E_WARNING7. It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would for a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.
Because include is a special language construct, parentheses are not needed around its argument. Take care when comparing return value.
Example #4 Comparing return value of include
E_WARNING9
E_ERROR0
Example #5 include and the return statement
E_ERROR2
E_ERROR3
E_ERROR4
include8
E_ERROR6
E_ERROR3
include8
E_ERROR9
include0
include1
include2
include3
include8
include5 is the value E_WARNING7 because the include was successful. Notice the difference between the above examples. The first uses return within the included file while the other does not. If the file can't be included, include7 is returned and E_WARNING is issued.
If there are functions defined in the included file, they can be used in the main file independent if they are before return or after. If the file is included twice, PHP will raise a fatal error because the functions were already declared. It is recommended to use include_once instead of checking if the file was already included and conditionally return inside the included file.
Another way to "include" a PHP file into a variable is to capture the output by using the Output Control Functions with include. For example:
Example #6 Using output buffering to include a PHP file into a string
require0
require1
include8
In order to automatically include files within scripts, see also the and configuration options in php.ini.
Note: Because this is a language construct and not a function, it cannot be called using variable functions, or .