Changes

Jump to: navigation, search

PHP, Filesystems and File I/O

807 bytes added, 19:33, 4 June 2007
Moving, Copying and Deleting Files with PHP
It is also possible to read the contents of an enter file with the ''readfile'' function. This function reads the entire contents of a file and outputs that content. Assumign you don't need to do anything but output the contents of a file then 'readfile'' is an easy solution because it does all the work for you. You do not need to open the file, read the data, close the file and display the data. All you need to do is call ''readfile'' and it does the rest.
 
== Checking in a File Exists ==
 
The ''file_exists'' fuinction can be used at any time to find if a file already exists in the file system. The function takes a single argument - the path to the file in question and returns a boolean ''true'' or ''false'' value depending on whether the existance of the file.
== Moving, Copying and Deleting Files with PHP ==
 
Files can be copied uysing the ''copy'' function, renamed using the ''rename'' function and deleted using the ''unlink'' function. For example we can perform a number tasks on our example file:
 
<pre>
<?php
 
if (file_exists('/tmp/php_essentials.txt)
{
copy ('/tmp/php_essentials.txt, '/tmp/php_essentials.bak'); // Copy the file
 
rename ('/tmp/php_essentials.bak', '/tmp/php_essentials.old'); // Rename the file
 
unlink ('/tmp/php_essentials.old'); // Delete the file
}
?>
</pre>

Navigation menu