Home arrow PHP arrow Page 6 - File And Directory Manipulation In PHP (part 2)

Move It - PHP

Now that you know the basics of reading and writing files, this second segment of our tutorial on the PHP filesystem API takes you into deeper waters, showing you how to copy, delete and rename files; scan directories; work with uploaded files over HTTP; perform pattern matches on file names; and read and write to processes instead of files.

  1. File And Directory Manipulation In PHP (part 2)
  2. Stripping It To The Bone
  3. Fertile Fields
  4. Configuring The System
  5. The Right Path
  6. Move It
  7. Beam Me Up
  8. Diving Into Directories
  9. A Pattern Emerges
  10. Purging The Dead
  11. Size Does Matter
  12. In Process
  13. Disk Full
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 65
August 21, 2003

print this article



In addition to offering you path information, PHP comes with a whole bunch of functions designed to simplify the task of moving, copying, renaming and deleting files on the filesystem. The first of these is the copy() function, which accepts two arguments, a source file and a destination, and copies the former to the latter. Here's an example which demonstrates:


// check to see if file exists
// if so, back it up
if (file_exists("matrix.txt"))
copy ("matrix.txt", "matrix.txt.orig") or die ("Could not copy file"); }


Note that if the destination file already exists, copy() will usually overwrite it. A failure to copy the file will cause copy() to return false; success returns true.

A corollary to the copy() function is the rename() function, which can be used to both rename and move files. Like copy(), it too accepts two arguments, a source file and a destination file. Consider the following example, which renames a file,


// check to see if file exists
// if so, rename it
if (file_exists("matrix.txt"))
rename ("matrix.txt", "neoworld.txt") or die ("Could not rename file"); }


and this one, which simultaneously renames and moves a file.


// check to see if file exists
// if so, move and rename it
if (file_exists("/home/john/newsletters"))
rename ("/home/john/newsletters", "/home/john/mail/lists") or die ("Could not move file"); }


It's possible to rename directories in the same manner as files - as illustrated in this next snippet:


// check to see if directory exists
// if so, rename it
if (is_dir("Mail"))
rename ("Mail", "mail-jun-03") or die ("Could not rename directory"); }


The rename() function comes in handy when you need to update files which are constantly being used by multiple processes. Instead of directly updating the target file with new data, rename() allows you to copy the original file contents into a new file, make your changes and then, once you're happy with the result, simply rename() the new file to the old one.

The following example demonstrates:


// read file
$contents = file("/etc/inittab") or die ("Could not read file");

// add a line to it
$contents[] = "x:5:respawn:/etc/X11/prefdm -nodaemon";

// write contents to temporary file
$tmpfile = tempnam("/tmp", "inittab.tmp.");

// open file
$fh = fopen ($tmpfile, "w") or die("Could not open file");

foreach ($contents as $line)
fwrite ($fh, $line) or die ("Could not write file");

// close file
fclose ($fh);

// move temporary file over original
rename($tmpfile, "/etc/inittab") or die ("Could not replace file");


Note my use of the tempnam() function above - this function generates a unique file name, given a directory and a filename prefix, and can help to avoid filename collisions between different processes.

When it comes time to delete files, PHP offers the unlink() function, which can be used to erase a file from the filesystem. Consider the following example, which demonstrates by deleting a specific file:


// check to see if file exists
// if so, erase it
if (file_exists("error.log"))
unlink ("error.log") or die ("Could not delete file");


You can also use the unlink() function to iterate over a directory and remove all the files within it - this is demonstrated in an example coming up shortly.

Note that the unlink() function (and indeed, all other file manipulation
functions) will fail if the user ID under which the Web server is running does not have adequate permissions to delete or otherwise modify the named file(s).

>>> More PHP Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: