Manipulating files is a basic necessity for serious programmers and PHP gives you a great deal of tools for creating, uploading, and editing files.
This section of the PHP tutorial is completely dedicated to how PHP can interact with files. After completing this section you should have a solid understanding of all types of file manipulation in PHP!
When you are manipulating files you must be very careful because you can do a lot of damage if you do something wrong. Common errors include editing the wrong file, filling a hard-drive with garbage data, and accidentally deleting a file's contents.
It is our hope that you will be able to avoid these and other slipups after reading this tutorial. However, we know that there are so many places where code can take a wrong turn, so we urge you to take extra care when dealing with files in PHP.
The presentation of the file lessons will begin with how to create, open, and close a file. After establishing those basics, we will then cover other important file tasks, such as: read, write, append, truncate, and uploading files with PHP.
The fopen() function is used to open files in PHP.
The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:
<html> <body> <?php $file=fopen("welcome.txt","r"); ?> </body> </html>
The file may be opened in one of the following modes:
Modes | Description |
---|---|
r | Read only. Starts at the beginning of the file |
r+ | Read/Write. Starts at the beginning of the file |
w | Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist |
w+ | Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist |
a | Append. Opens and writes to the end of the file or creates a new file if it doesn't exist |
a+ | Read/Append. Preserves file content by writing to the end of the file |
x | Write only. Creates a new file. Returns FALSE and an error if file already exists |
x+ | Read/Write. Creates a new file. Returns FALSE and an error if file already exists |
Note: If the fopen() function is unable to open the specified file, it returns 0 (false).
Example : The following example generates a message if the fopen() function is unable to open the specified file:
<html> <body> <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); ?> </body> </html>
The fclose() function is used to close an open file:
<?php $file = fopen("test.txt","r"); //some code to be executed fclose($file); ?>
The feof() function checks if the "end-of-file" (EOF) has been reached.
The feof() function is useful for looping through data of unknown length.
Note: You cannot read from files opened in w, a, and x mode!
if (feof($file)) echo "End of file";
The fgets() function is used to read a single line from a file.
Note: After a call to this function the file pointer has moved to the next line.
Example : The example below reads a file line by line, until the end of file is reached:
<?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached while(!feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?>
The fgetc() function is used to read a single character from a file.
Note: After a call to this function the file pointer moves to the next character.
Example : The example below reads a file character by character, until the end of file is reached:
<?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?>
PHP: indicates the earliest version of PHP that supports the function.
Function | Description | PHP Version |
---|---|---|
basename() | Returns the filename component of a path | 3 |
chgrp() | Changes the file group | 3 |
chmod() | Changes the file mode | 3 |
chown() | Changes the file owner | 3 |
clearstatcache() | Clears the file status cache | 3 |
copy() | Copies a file | 3 |
delete() | See unlink() or unset() | 3 |
dirname() | Returns the directory name component of a path | 4 |
disk_free_space() | Returns the free space of a directory | 4 |
disk_total_space() | Returns the total size of a directory | 3 |
diskfreespace() | Alias of disk_free_space() | 3 |
fclose() | Closes an open file | 3 |
feof() | Tests for end-of-file on an open file | 4 |
fflush() | Flushes buffered output to an open file | 3 |
fgetc() | Returns a character from an open file | 3 |
fgetcsv() | Parses a line from an open file, checking for CSV fields | 3 |
fgets() | Returns a line from an open file | 3 |
fgetss() | Returns a line, with HTML and PHP tags removed, from an open file | 3 |
file() | Reads a file into an array | 3 |
file_exists() | Checks whether or not a file or directory exists | 4 |
file_get_contents() | Reads a file into a string | 5 |
file_put_contents | Writes a string to a file | 3 |
fileatime() | Returns the last access time of a file | 3 |
filectime() | Returns the last change time of a file | 3 |
filegroup() | Returns the group ID of a file | 3 |
fileinode() | Returns the inode number of a file | 3 |
filemtime() | Returns the last modification time of a file | 3 |
fileowner() | Returns the user ID (owner) of a file | 3 |
fileperms() | Returns the permissions of a file | 3 |
filesize() | Returns the file size | 3 |
filetype() | Returns the file type | 3 |
flock() | Locks or releases a file | 4 |
fnmatch() | Matches a filename or string against a specified pattern | 3 |
fopen() | Opens a file or URL | 3 |
fpassthru() | from an open file, until EOF, and writes the result to the output buffer | 5 |
fputcsv() | Formats a line as CSV and writes it to an open file | 3 |
fputs() | Alias of fwrite() | 3 |
fread() | Reads from an open file | 4 |
fscanf() | Parses input from an open file according to a specified format | 3 |
fseek() | Seeks in an open file | 4 |
fstat() | Returns information about an open file | 3 |
ftell() | Returns the current position in an open file | 4 |
ftruncate() | Truncates an open file to a specified length | 3 |
fwrite() | Writes to an open file | 4 |
glob() | Returns an array of filenames / directories matching a specified pattern | 3 |
is_dir() | Checks whether a file is a directory | 3 |
is_executable() | Checks whether a file is executable | 3 |
is_file() | Checks whether a file is a regular file | 3 |
is_link() | Checks whether a file is a link | 3 |
is_readable() | Checks whether a file is readable | 3 |
is_uploaded_file() | Checks whether a file was uploaded via HTTP POST | 4 |
is_writable() | Checks whether a file is writeable | 3 |
is_writeable() | Alias of is_writable() | 3 |
link() | Creates a hard link | 3 |
linkinfo() | Returns information about a hard link | 3 |
lstat() | Returns information about a file or symbolic link | 3 |
mkdir() | Creates a directory | 4 |
move_uploaded_file() | Moves an uploaded file to a new location | 4 |
parse_ini_file() | Parses a configuration file | 4 |
pathinfo() | Returns information about a file path | 3 |
pclose() | Closes a pipe opened by popen() | 3 |
popen() | Opens a pipe | 3 |
readfile() | Reads a file and writes it to the output buffer | 3 |
readlink() | Returns the target of a symbolic link | 4 |
realpath() | Returns the absolute pathname | 3 |
rename() | Renames a file or directory | 3 |
rewind() | Rewinds a file pointer | 3 |
rmdir() | Removes an empty directory | 3 |
set_file_buffer() | Sets the buffer size of an open file | 3 |
stat() | Returns information about a file | 3 |
symlink() | Creates a symbolic link | 3 |
tempnam() | Creates a unique temporary file | 3 |
tmpfile() | Creates a unique temporary file | 3 |
touch() | Sets access and modification time of a file | 3 |
umask() | Changes file permissions for files | 3 |
unlink() | Deletes a file | 3 |
All Rights Reserved. © 2024 BookOfNetwork