RCRAMER.COM
Clever website slogan
Home > Tech > PHP

PHP File Manipulation

An array stores multiple values in one single variable.

Also see w3schools.com - Filesystem Functions

Opening a file

The fopen() fucntion 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:

<?php
$file=fopen("hello.txt","r");
?>
ModesDescription
r Read only. Starts at beginning of file.
r+Read/Write. Starts at beginning of file.
w Write only. Create new file or overwrite existing file.
w+Read/Write. Create new file or overwrite existing file.
a Append. Add to end of file if it exists or create a new file.
a+Read/Append. Preserve file contents by writing to end of 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() is unable to open the file, it returns 0 (false).

Closing a file

The fclose() function closes an open file. <?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>

Reading a file line by line

The fgets() functions is used to read a single line form a file. Note: after a call to this function the file pointer has moved to the next line.

<?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);
?>

Writing to a file

Use the fwrite() function to write to an open file. The function returns the number of bytes written or FALSE on failure.

fwrite(file,string,length)

Note: This function is binary-safe (meaning that both binary data, like images, and character data can be written with this function).

<?php
$file = fopen("welcome.txt", "w") or exit("Unable to open file!"); echo fwrite($file,"Hello World");
fclose($file);
?>

Exclussive access to a file

The flock() function locks or releases a file. This function returns TRUE on success or FALSE on failure.

flock(file,lock,block)
ParameterDescription
file Required. Specifies an open file to lock or release.
lock Required. Specifices what kind of lock to use.
  • LOCK_SH - Shared lock (reader). Allow other prosses to access the file.
  • LOCK_EX - Exclusive lock (writer). Prevent other prosses from accessing the file
  • LOCK_UN - Release a shard or exclusive lock
  • LOCK_NB - Avoids blocking other prosses when locking
blockOptional. Set to 1 to block other prosses while locking

Note: These locks only apply to the current PHP prosses. Other prosses can modify or delete a PHP-locked file if permissions allow. flock() is mandatory under windows. The lock is automatically released, by fclose(), when the script is finished.

Rename a file

Use the rename() function to rename a file or directory.

rename(oldname,newname)

Delete a file

The unlink() function deletes a file. The function returns TRUE on success and FALSE on failure.

<?php
$file = "welcome.txt";
if (!unlink($file)) {
  echo ("Error deleting $file"); } else {
  echo ("Deleted $file");
}
?>

File permissions

See fperms(), chgrp(), chmod(), chown() and stat()