PHP & MySQL Tutorial
File Handling - Delete and Copy
Before we access any directory, we must open the directory with the opendir() function. It takes the full path of the directory as its argument and returns a directory handle.
After we open the directory, a loop can assign all the file names to a variable list by using the readdir() function.
After we loop through all the files in the directory, we should close the directory using closedir() function.
The example below assigns each file name in PHP folder to a variable called $file, then it adds it to the $fileList variable.
<?php $dirName = "C:/PHP"; $fileList = null; $dir = opendir($dirName); while ($file = readdir($dir)) { if( $file != "." and $file !="..") { $fileList .="<li>$file</li>"; } } closedir($dir); ?> <html> <head><title>File List</title></head> <body> <p>Files in <?php echo $dirName ; ?> </p> <ul> <?php echo $fileList; ?> </ul> </body> </html>
The copy requires two arguments:
- Full path of the source file to be copied.
- Full path of the destination.
It returns a true if the copy is successful, otherwise it returns false.
The following example is trying to copy news file from C:/PHP directory. Then, it tells if it succeeded in copying.
<?php $source = "C:/PHP/news.txt"; $destination = "C:/PHP/news.bak"; if( copy($source,$destination) ) $message = "Copies $source<br /> to $destination"; else $message = "Failed copying $source"; ?> <html> <head><title>File Copy</title></head> <body> <?php echo $message; ?> </body> </html>
We can rename a file using rename() function. It requires two arguments:
- Old file name
- New file name
It returns true for successful renaming, otherwise it returns false.
The following example renames the news.bak to news.old.
<?php $old = "C:/PHP/news.bak"; $new = "C:/PHP/news.new"; if( rename($old,$new) ) $message = "Renamed $old<br /> to $new"; else $message = "Failed copying $old"; ?> <html> <head><title>File Rename</title></head> <body> <?php echo $message; ?> </body> </html>
The unlink() function deletes the files given as its argument. The following example demonstrates how it works.
<?php function file_delete($file) { if( unlink($file) ) echo "$file<br />deleted. <hr>"; else echo "Could not delete $file <hr>"; } ?> <html> <head><title>File Delete</title></head> <body> <?php $fileA = "C:/php/news.bak"; $fileB = "C:/php/news.bak"; file_delete($fileA); file_delete($fileB); ?> </body> </html>
The fopen() function is used to read text from files, write text to files and append text to files. It needs two arguments:
- File name
- Mode
Following table shows the file mode:
Mode | Description |
---|---|
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. |
a | Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
a+ | Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. |
x | Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. |
x+ | Create and open for reading and writing; otherwise it has the same behavior as 'x'. |
c | Open the file for writing only. If the file does not exist, it is created. If it exists, it is neither truncated (as opposed to 'w'), nor the call to this function fails (as is the case with 'x'). The file pointer is positioned on the beginning of the file. This may be useful if it's desired to get an advisory lock (flock()) before attempting to modify the file, as using 'w' could truncate the file before the lock was obtained (if truncation is desired, ftruncate() can be used after the lock is requested). |
c+ | Open the file for reading and writing; otherwise it has the same behavior as 'c'. |
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization