PHP some filesystem functions

PHP have many functions to acces and manipulate files, the most important filesystem functions are posted here...

basename() : Returns the filename component from a path.
<?php
$path = "/directory1/index.html";
echo basename($path);
// display: index.html
echo basename($path ,".html" ); // display: index
?>

file_exists() : Checks if a file or directory exists ot not.
<?php
echo file_exists("test.txt"); // returns True if exists, otherwise returns False
?>

rename() : Renames a file or directory.
<?php
rename("dir1","dir2"); // You can rename a file or a folder
?>

copy() : Copies a file.
<?php
copy("sourcefile.txt","destinationfile.txt");
?>

unlink() : Deletes a file.
<?php
unlink("file.txt"); // returns True if deleted successfully, False if not deleted.
?>



filesize() : Returns the size of the file.
<?php
echo filesize("file.txt"); // returns the size of file.txt in bytes, or False in failure.
?>

filetype() : Returns the file type.
<?php
echo filetype("file.txt"); // returns type: file.
//Possible types are: file, dir, link, block, char, fifo, unknown.
?>

is_readable() : Checks whether a file is readable, returns true if yes.
is_writable() : Checks whether a file is writeable, returns true if yes.

fileperms() : Returns the permissions of a file.
filemtime() : Returns the last modification time of a file, Display a number in second, to convert it to humain redable date format, use:
<?php
echo date("F d Y H:i:s", filemtime("test.txt")); // returns date format: September 03 2008 18:27:35
?>


If you have any question, or need more details or functions, post a comment here.

1 comments:

Anonymous said...

Hi,

I'm Alisha from Wowzio, and I'm excited to tell you about our new widget platform that helps bloggers increase readership by providing engaging widgets containing your blog's rich content. You can check out widgets customized for your blog here:

Wowzio Widgets for your Blog

I wanted to reach out to you to ask for your feedback on these widgets (feel free to install them on your blog, if you feel they are a good fit). I'm sorry for leaving this message via a comment, it's not at all our intent to spam you ( which is why i'm leaving this comment on an older post and you can always remove this comment ). Again, we would love to hear your feedback.

Thanks,
Alisha Wright
alisha.wright1@gmail.com