php » Folder and File Functions

Check File Permissions

$path = “/mypath/myfile.txt”
$stat = stat($path);
print_r(posix_getpwuid($stat['uid']));

// will return something like:
array(7) {
["name"]=>
string(13) “php.net”
["passwd"]=>
string(1) “x”
["uid"]=>
int(148864)
["gid"]=>
int(148910)
["gecos"]=>
string(13) “php.net”
["dir"]=>
string(25) “/home/sites/php.net”
["shell"]=>
string(13) “/sbin/nologin”
}

Posted by Mark on February 18th, 2010 under Folder and File Functions, php  •  No Comments

Copy Directory Contents In To Another Directory

function copy_files($old, $new){
if ($handle = opendir($old)) {
while($file = readdir($handle)) {
if ($file != “.” && $file != “..” && $file != “_archive”) {
if(copy($old . $file, $new . $file)) {
echo “$file copied successfully.”;
}
}
}
closedir($handle);
}
}

Posted by Mark on November 2nd, 2009 under Folder and File Functions  •  No Comments

Empty A Directory

function empty_directory($dir){
if ($handle = opendir($dir)){
$array = array();
while (false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “..”) {
if(is_dir($dir.$file)){
if(!@rmdir($dir.$file)){ // Empty directory? Remove it
empty_directory($dir.$file.’/'); // [...]

Posted by Mark on November 2nd, 2009 under Folder and File Functions, php  •  No Comments

List all files in a folder

This function will loop through a given folder and list the file contained within it. This one has been set to ignore subdirectories.

$dirpath=PATH_TO_FILE;
$dh = opendir($dirpath);
while (false !== ($file = readdir($dh))) {
//Don’t list subdirectories
if (!is_dir(”$dirpath/$file”)){
$file_name = htmlspecialchars(ucfirst(preg_replace(’/\..*$/’, ”, $file)));
[...]

Posted by Mark on April 18th, 2009 under Folder and File Functions  •  No Comments