Web Development Course:
In developing, functions are tools that you combine with your skills to achieve the desired effect. For example, using any PHP file function, you can quickly and easily read a file into an array, be it characters or binary data such as pictures.
The navigation of the filesystem becomes effortless when you know which PHP file function needs to be applied in certain situations. In this tutorial, we will provide you with a handy list of functions used to manipulate PHP filesystems and files.
We will review the most frequently used method: the PHP file() function, applied when it is necessary to read a file into an array. Additionally, you will learn the techniques to change groups, modes, and owners of files.
PHP File Function: Main Tips
- PHP file functions allow accessing and manipulating filesystems.
- These functions are inbuilt into the PHP core.
- When using this function on Unix, forward slash (/) is used to separate directories.
- On Windows, forward (/) and backward (\) slashes may be used for that.
- One of the most important methods is the PHP file() function, used to read a file into array.
Most Common Method: file()
As we have mentioned, among the most used methods stands the PHP file function that reads a file into an array. Each element of the array will represent a line from a file. Before PHP 4.3, using
file() PHP could only read character data, but now it also works with binary data such as pictures.
Let's view the correct syntax that is needed for PHP file function to be executed correctly:
Only the first of the parameters is required, as it names the exact file for PHP
file() to read.
Options for Runtime Configuration
The way PHP filesystem functions behave is inevitably affected by the settings located in php.ini. Let's see the different options we can use for the configuration:
|allow_url_fopen||"1"||Allow fopen()-type functions to work with URL addresses (implemented in PHP 4.0.4)||PHP_INI_SYSTEM|
|user_agent||NULL||Define user agent for PHP to send (available since PHP 4.3)||PHP_INI_ALL|
|default_socket_timeout||"60"||Set default timeout, in seconds, for socket based streams (implemented in PHP 4.3)||PHP_INI_ALL|
|from||""||Define anonymous FTP password (your email address)||PHP_INI_ALL|
|auto_detect_line_endings||"0"||When set to the value of 1, the script will examine data read by fgets() and file() PHP functions to see whether it is using any Unix, MS-Dos or Mac line-ending characters (implemented in PHP 4.3)||PHP_INI_ALL|
List of Functions
In the table below, you have the list of functions PHP provides to make your work with the PHP filesystem easier.
Some of them (such as fopen()) you might also recognize from our previous lessons, in which we learned to work with PHP code to open external files:
|basename()||Return filename component of path|
|chgrp()||Change file group|
|chmod()||Change file mode|
|chown()||Change file owner|
|clearstatcache()||Clear file status cache|
|delete()||See unlink() or unset()|
|dirname()||Return directory name component of path|
|disk_free_space()||Return free space of directory|
|disk_total_space()||Return total size of directory|
|diskfreespace()||Alias of disk_free_space()|
|fclose()||Close open file|
|feof()||Test for end-of-file on open file|
|fflush()||Flush buffered output to an open file|
|fgetc()||Return character from open file|
|fgetcsv()||Parse line from open file, checking for CSV fields|
|fgets()||Return line from open file|
|fgetss()||Return line, with HTML and PHP tags removed, from open file|
|file()||Read file into array|
|file_exists()||Check whether or not file or directory exists|
|file_get_contents()||Read file into string|
|file_put_contents()||Write string to file|
|fileatime()||Return last access time of file|
|filectime()||Return last change time of file|
|filegroup()||Return group ID of file|
|fileinode()||Return inode number of file|
|filemtime()||Return last modification time of file|
|fileowner()||Return user ID (owner) of file|
|fileperms()||Return permissions of file|
|filesize()||Return file size|
|filetype()||Return file type|
|flock()||Lock or release file|
|fnmatch()||Matche filename or string against specified pattern|
|fopen()||Open file or URL|
|fpassthru()||Read from open file, until EOF, and write result to output buffer|
|fputcsv()||Format line as CSV and write to an open file|
|fputs()||Alias of fwrite() function|
|fread()||Read from open file|
|fscanf()||Parse input from open file according to specified format|
|fseek()||Seek in open file|
|fstat()||Return info about open file|
|ftell()||Return current position in open file|
|ftruncate()||Truncate open file to specified length|
|fwrite()||Write to an open file|
|glob()||Return array of filenames / directories matching specified pattern|
|is_dir()||Check whether a file is directory|
|is_executable()||Check whether a file is executable|
|is_file()||Check whether a file is regular file|
|is_link()||Check whether a file is link|
|is_readable()||Check whether file is readable|
|is_uploaded_file()||Check whether file was uploaded via HTTP POST|
|is_writable()||Check whether file is writeable|
|is_writeable()||Alias of is_writable() function|
|lchgrp()||Change group ownership of symlink|
|lchown()||Change user ownership of symlink|
|link()||Create hard link|
|linkinfo()||Return info about hard link|
|lstat()||Return info about file or symbolic link|
|move_uploaded_file()||Move uploaded file to new location|
|parse_ini_file()||Parse configuration file|
|parse_ini_string()||Parse configuration string|
|pathinfo()||Return info about file path|
|pclose()||Close pipe opened by popen()|
|readfile()||Read file and write to output buffer|
|readlink()||Return target of symbolic link|
|realpath()||Return absolute pathname|
|realpath_cache_get()||Return realpath cache entries|
|realpath_cache_size()||Return realpath cache size|
|rename()||Rename file or directory|
|rewind()||Rewind file pointer|
|rmdir()||Remove empty directory|
|set_file_buffer()||Set buffer size of open file|
|stat()||Returns information about file|
|symlink()||Create symbolic link|
|tempnam()||Create unique temporary file|
|tmpfile()||Create unique temporary file|
|touch()||Set access and modification time of file|
|umask()||Change file permissions for files|
PHP File Function: Summary
- Inbuilt PHP functions that are used to access and manipulate filesystems can simply called filesystem functions. They don't require installation.
- Directories are separated using forward slashes (/). On Windows you can use backward slashes (\) as well.
- PHP file function is probably used the most. You can read a file into array using it. PHP
file()is able to read both binary data and characters.
PHP FTP: A Handy Guide for Working with FTP Servers
Follow this PHP FTP tutorial and find out how to use PHP create directory and PHP get current directory functions easily with this PHP FTP tutorial.
Correct Usage of PHP ZIP: Set up Guide and Function List Included
Tips and tricks on PHP Zip files: learn all about downloading PHP Zip extension and find out how to use PHP Zip files easily with examples.