Stream Me Up, Scotty (part 2) - To Create And Destroy (
Page 3 of 6 )
Next
up, a little file and directory manipulation. If you'd like to create
directories, PHP provides the mkdir() function, which accepts a file path and a
mode as parameters, So, if I wanted to create a directory called "/tmp/me" with
permissions 755, I would use something like this:
<?
if (mkdir ("/tmp/me", 0755))
{
echo "Successful!";
}
else
{
echo "Unsuccessful";
}
?>
And to reverse the process, there's always the rmdir()
function, used to delete directories. Note that rmdir() will fail if the
directory permissions don't allow deletion, or if the directory contains one or
more files.
<?
if (rmdir ("/tmp/me"))
{
echo "Successful!";
}
else
{
echo "Unsuccessful - check permissions and ensure directory is empty!";
}
?>
You can delete files with the unlink() function, which
accepts a filename as parameter. <? if (unlink("/tmp/dummyfile")) { echo
"Successful!"; } else { echo "Unsuccessful!"; } ?>
And finally, you can copy files with the copy()
function,
<?
if (copy("/tmp/source", "/tmp/destination"))
{
echo "Successful!";
}
else
{
echo "Unsuccessful!";
}
?>
and rename (or move) them with the rename() function.
<?
if (rename("/tmp/oldfile", "/etc/newfile"))
{
echo "Successful!";
}
else
{
echo "Unsuccessful!";
}
?>
This article copyright Melonfire
2000. All rights reserved.