This post covers the Files section of the Input/Output chapter when studying for the Zend PHP 7 Certification.
PHP has several functions for creating, reading, uploading, and editing files.
The two main types of functions that PHP use are listed below.
fopen() function opens a file or a URL. It takes two parameters.
The modes that can be used with the function can be seen below.
r– Open for reading only; place the file pointer at the beginning of the file.
r+– Open for reading and writing; place the file pointer at the beginning of the file.
w– Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
w+– Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
<?php $handle = fopen("/home/user/file.txt", "r"); $handle = fopen("/home/user/file.gif", "wb"); $handle = fopen("http://www.example.com/", "r"); $handle = fopen("ftp://user:email@example.com/somefile.txt", "w");
It should be noted that using
w mode will not update the modification time of a file like you may expect.
fread() function takes two parameters – handle and length.
fread() reads up to length bytes from the file pointer referenced by handle. The file reading stops as soon if conditions are met such as the following.
<?php $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle);
fclose() function closes an open file pointer. It’s good practice to close all files after you have finished with them.
<?php $handle = fopen('somefile.txt', 'r'); fclose($handle);
file_get_contents() function reads an entire file into a string. It can take up to 5 parameters.
FILE_USE_INCLUDE_PATHconstant can be used to trigger include path search.
<?php $homepage = file_get_contents('http://www.example.com/'); echo $homepage;
To read certain sections of files, pass in the offset as the fourth parameter starting from
NULL for the second and third parameters if you do not wish to change them.
<?php // Read 14 characters starting from the 21st character $section = file_get_contents('people.txt', NULL, NULL, 20, 14); var_dump($section);
file_get_contents() function is similar to
file() function, except that
file_get_contents() returns the contents as a string and returns false on failure.
file() reads an entire file into an array.
As oh PHP version 7.1, support for negative offsets has been added for this function.
file_put_contents() function writes a string to a file.
Identically to calling
fclose() successively to write data to a file. If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the
FILE_APPEND flag is set. There are three parameters that are used in the function.
FILE_APPENDappend the data to the file rather than overwriting it.
<?php $file = 'people.txt'; // The new person to add to the file $person = "John Smith\n"; // Write the contents to the file, // using the FILE_APPEND flag to append the content to the end of the file // and the LOCK_EX flag to prevent anyone else writing to the file at the same time file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
Note: This article is based on PHP version 7.1.