May 2011

Competition announcements will be made in here.
Locked
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

May 2011

Post by jacek »

This competition is now over.

All of the entries that got the right idea are posted below.

the winning entry was submitted by Tino (the moderator) and is shown below.
<?php

header('Content-Type: text/plain');

function new_scandir($dir) {
   $files = array();

   $x = 0;

   foreach ( glob("{$dir}*") as $file ) {
       $files[$x] = array();

       // use of stat function to minimize amount of function calls like fileowner()
       // using this  i could've added a lot more to the array but that would've gotten ridiculous
       // i mean, device number? pointless :p
       $stats = stat($file);

       $files[$x]['path']      = $file;
       $files[$x]['name']      = basename($file);
       $files[$x]['in_dir']    = dirname($file);
       $files[$x]['type']      = filetype($file);

       if ( function_exists('finfo_file') ) {
           if ( phpversion() >= '5.3.0' ) {
               $info = finfo_open(FILEINFO_MIME_TYPE);
               $files[$x]['mime_type'] = finfo_file($info, $file);
           } else {
               $info = finfo_open(FILEINFO_MIME);
               $files[$x]['mime_type'] = finfo_file($info, $file);
           }

           finfo_close($info);
       } else {
           $files[$x]['mime_type'] = mime_content_type($file);
       }

       // doesn't work on windows
       if ( function_exists('posix_getgrgid') ) {
           $info = posix_getgrgid(filegroup($file));
           $files[$x]['group'] = $info['name'];
       } else {
           $files[$x]['group_id'] = filegroup($file);
       }

       // doesn't work on windows
       if ( function_exists('posix_getpwuid') ) {
           $info = posix_getpwuid(fileowner($file));
           $files[$x]['owner'] = $info['name'];
       } else {
           $files[$x]['owner_id'] = $stats['uid'];
       }
       
       $files[$x]['created']   = date('d/m/Y h:i:s', $stats['ctime']);
       $files[$x]['last_mod']  = date('d/m/Y h:i:s', $stats['mtime']);

       if ( is_file($file) ) {
           $files[$x]['size'] = (int) ($stats['size'] / 1024) . 'kB';
       }

       if ( is_dir($file) ) {
           $files[$x]['dir'] = new_scandir($file.'/');
       }

       $x++;
   }
   
   return $files;
}

print_r(new_scandir('./'));

?>
Task:
- To create a function similar to scandir()
- As much information on each file and folder should be found as you can work out how to do
- The winner will be the person who finds the most things.

Rules:
- You can use any PHP function, but no external programs.
- No cheating.

Prize:
- Any game / codecanyon item under £10, see previous announcement for more info.
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: May 2011

Post by jacek »

<?php

header('Content-Type: text/plain');

function file_info($path)
{
   if (file_exists($path) === false)
   {
       return false;
   }
   
   $file    = end(explode('/', $path));
   
   $info    = array();
   $a       = explode('.', $file);
   
   $data['name'] = '';
   
   for ($x=0;$x<=count($a);++$x)
   {
       if ($x == count($a) - 1)
       {
           break;
       }
       
       if ($x == count($a) - 2)
       {
           $data['name'] .= $a[$x];
       }
       else
       {
           $data['name'] .= $a[$x] . '.';
       }
   }
   
   $data['type']        = end ($a);
   $data['size']        = filesize($path);
   $data['last_mod']    = filemtime ($path);
   
   return $data;
}

function dir_scan ($path)
{
   if(is_dir($path) === false)
   {
       return false;
   }
   
   $files = scandir($path);
       
   $data = array();
       
   foreach ($files as $file)
   {
       $is_dir = is_dir("${path}/${file}/");
   
       if($file !== '.' && $file !== '..')
       {    
           if($is_dir === false)
           {
               $data[] = file_info("{$path}/{$file}");
           }
           else
           {
               $data[$file] = dir_scan("${path}/${file}");
           }
       }        
   }

   return $data;
}

print_r(dir_scan('./'));

?>
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: May 2011

Post by jacek »

<?php

header('Content-Type: text/plain');

function get_directory($directory, $protection = FALSE){
   if($protection === "PROTECT_FILE_DIRECTORY"){
       $directory        = htmlentities(trim($directory, "/"));
   }else{
       $directory        = trim($directory, "/");
   }
       $file            = glob("{$directory}/*");
       $files            = array();
       $count_files    = count($file);
       $folder_count    = 0;
       $file_count        = 0;
       
   $files['directory_info']['directory_name']    = $directory;
   $files['directory_info']['total_count']        = $count_files;
   
   foreach($file as $file){        
       $file_name        = explode("/", $file);
       $file_update    = date("l F j H:i:s", filemtime($file));
       
       if(is_dir($file)){
           $file_type    = "Folder";
           $file_size    = count(glob("{$file}/*"))." File(s)";
           ++$folder_count;
       }else{
           $file_type    = end(explode(".", $file_name[1]));
           $file_size    = filesize($file)." Bytes";
           ++$file_count;
       }
           
       $files[]    = array(
           "name"            => $file_name[1],
           "size"            => $file_size,
           "file_type"        => $file_type,
           "last_update"    => $file_update,
           "relative_path"    => $file,
           "full_path"        => realpath($file)
           );
   }
   
   $files['directory_info']['folder_count']    = $folder_count;
   $files['directory_info']['file_count']        = $file_count;
   
   return $files;
}

print_r(get_directory('./'));

?>
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: May 2011

Post by jacek »

<?php

header('Content-Type: text/plain');

define("FILE"," file");
define("FOLDER","Folder");
function scandirectory($dir) {
   $dir = str_replace('%20', '', $dir);
   if(!is_dir($dir)) {
     return false;
   }
   $dir_files = glob($dir.'/*');
   if(count($dir_files) == 0) {
     return false;
   }
   $files_in_dir['total']=count($dir_files);
   $i = 0;
   foreach($dir_files as $file) {
       $files_in_dir[$i]['name'] = substr($file,strlen($dir)+1);
       if(is_dir($file)) {
         $files_in_dir[$i]['type'] = FOLDER;
         $files_in_dir[$i]['filesindir'] = count(glob($file.'/*'));
       } else {
         $files_in_dir[$i]['size'] = filesize($file);
         $files_in_dir[$i]['type'] = pathinfo($file, PATHINFO_EXTENSION).FILE;
         $files_in_dir[$i]['ext'] = pathinfo($file, PATHINFO_EXTENSION);
         
         $files_in_dir[$i]['contents'] = file_get_contents($file);
       }
       $files_in_dir[$i]['modifiedtime'] = filemtime($file);
       $files_in_dir[$i]['modifieddate'] = date("d F Y H:i .", filectime($file));
       

       $i++;
   }
   return($files_in_dir);
}

print_r(scandirectory('./'));

?>
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: May 2011

Post by jacek »

<?php

header('Content-Type: text/plain');

function ls($path, $hidden_files = true, $dir_size = true, $descending_order = false){
   if(!is_dir($path)){
       return false;
   }
   
   $descending_order    = (int) $descending_order;
   $files            = scandir($path, $descending_order);
   unset($files[0], $files[1]);
   chdir($path);
   $ls = array();
   
   clearstatcache();
   foreach($files as $filename){
       if(!$hidden_files && substr($filename, 0, 1) == '.'){
           continue;
       }
       $file = array(
           'name'    => $filename,
           'full_path' => realpath($filename)
       );
       
       $perms = fileperms($filename);
       if (($perms & 0xC000) == 0xC000) {
           $info = 's';
       } elseif (($perms & 0xA000) == 0xA000) {
           $info = 'l';
       } elseif (($perms & 0x8000) == 0x8000) {
           $info = '-';
       } elseif (($perms & 0x6000) == 0x6000) {
           $info = 'b';
       } elseif (($perms & 0x4000) == 0x4000) {
           $info = 'd';
       } elseif (($perms & 0x2000) == 0x2000) {
           $info = 'c';
       } elseif (($perms & 0x1000) == 0x1000) {
           $info = 'p';
       } else {
           $info = 'u';
       }
       $info .= (($perms & 0x0100) ? 'r' : '-');
       $info .= (($perms & 0x0080) ? 'w' : '-');
       $info .= (($perms & 0x0040) ?
                   (($perms & 0x0800) ? 's' : 'x' ) :
                   (($perms & 0x0800) ? 'S' : '-'));
       $info .= (($perms & 0x0020) ? 'r' : '-');
       $info .= (($perms & 0x0010) ? 'w' : '-');
       $info .= (($perms & 0x0008) ?
                   (($perms & 0x0400) ? 's' : 'x' ) :
                   (($perms & 0x0400) ? 'S' : '-'));
       $info .= (($perms & 0x0004) ? 'r' : '-');
       $info .= (($perms & 0x0002) ? 'w' : '-');
       $info .= (($perms & 0x0001) ?
                   (($perms & 0x0200) ? 't' : 'x' ) :
                   (($perms & 0x0200) ? 'T' : '-'));
       $file['permissions'] = substr(sprintf('%o', $perms), -4);
       $file['full_permissions'] = $info;
       
       $owner = posix_getpwuid(fileowner($filename));
       $group = posix_getgrgid(filegroup($filename));
       $file['owner_group'] = $owner['name'] . ':' . $group['name'];
       
       if(is_dir($filename)){
           $file['type']    = 'dir';
           $total_size    = 0;
           if($dir_size){
               $original_dir = getcwd();
               chdir($filename);
               foreach(scandir('.') as $subfile){
                   $total_size += filesize($subfile);
               }
               chdir($original_dir);
               unset($original_dir);
           }
           
           $file['size'] = $total_size;
       }else {
           $file['type'] = 'file';
           $file['size'] = filesize($filename);
       }
       
       $size = $file['size'];
       $units = array(
           'B',
           'KB',
           'MB',
           'GB',
           'TB'
       );
       for ($i = 0; $size > 1024; $i++) {
           $size /= 1024;
       }
       $file['human_size'] = round($size, 2) . ' ' . $units[$i];
       
       if(!function_exists('mime_content_type')){
           $mime_types = array(
               'txt' => 'text/plain',
               'htm' => 'text/html',
               'html' => 'text/html',
               'php' => 'text/html',
               'css' => 'text/css',
               'js' => 'application/javascript',
               'json' => 'application/json',
               'xml' => 'application/xml',
               'swf' => 'application/x-shockwave-flash',
               'flv' => 'video/x-flv',

               'png' => 'image/png',
               'jpe' => 'image/jpeg',
               'jpeg' => 'image/jpeg',
               'jpg' => 'image/jpeg',
               'gif' => 'image/gif',
               'bmp' => 'image/bmp',
               'ico' => 'image/vnd.microsoft.icon',
               'tiff' => 'image/tiff',
               'tif' => 'image/tiff',
               'svg' => 'image/svg+xml',
               'svgz' => 'image/svg+xml',

               'zip' => 'application/zip',
               'rar' => 'application/x-rar-compressed',
               'exe' => 'application/x-msdownload',
               'msi' => 'application/x-msdownload',
               'cab' => 'application/vnd.ms-cab-compressed',

               'mp3' => 'audio/mpeg',
               'qt' => 'video/quicktime',
               'mov' => 'video/quicktime',

               'pdf' => 'application/pdf',
               'psd' => 'image/vnd.adobe.photoshop',
               'ai' => 'application/postscript',
               'eps' => 'application/postscript',
               'ps' => 'application/postscript',

               'doc' => 'application/msword',
               'rtf' => 'application/rtf',
               'xls' => 'application/vnd.ms-excel',
               'ppt' => 'application/vnd.ms-powerpoint',

               'odt' => 'application/vnd.oasis.opendocument.text',
               'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
           );

           $ext = strtolower(array_pop(explode('.',$filename)));
           if (array_key_exists($ext, $mime_types)) {
               return $mime_types[$ext];
           }
           elseif (function_exists('finfo_open')) {
               $finfo = finfo_open(FILEINFO_MIME);
               $mimetype = finfo_file($finfo, $filename);
               finfo_close($finfo);
               return $mimetype;
           }
           else {
               return 'application/octet-stream';
           }
       }else {
           $file['mimetype'] = mime_content_type($filename);
       }
       
       if(substr($file['mimetype'], 0, 5) == 'image'){
           $dimensions = getimagesize($filename);
           if($dimensions !== false){
               $file['dimensions'] = array(
                   'width' => $dimensions[0],
                   'height' => $dimensions[1]
               );
           }
       }
       
       $stat = stat($filename);
       
       $filemtime = $stat['mtime'];
       $fileatime = $stat['atime'];
       $file['last_modified'] = $filemtime;
       $file['human_last_modified'] = date ("F d Y H:i:s", $filemtime);
       $file['last_accessed'] = $fileatime;
       $file['human_last_accessed'] = date ("F d Y H:i:s", $fileatime);
       
       $ls[] = $file;
   }
   
   return $ls;
}

print_r(ls('./'));

?>
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: May 2011

Post by jacek »

<?php

function files($dir) {
   $files = scandir($dir);
   foreach ($files as $file)
   {
       @$size = filesize($file);
       @$time = date("d/m/Y - H:i:s", filemtime($file));
       if ($size < 1000) $size = $size." B";
       else if ($size > 1000  && $size <1000000) $size = (int)($size/1000)." KB";
       else if ($size > 1000000  && $size <1000000000) $size = (int)($size/1000000)." MB";
       else if ($size > 1000000000) $size = (int)($size/1000000000)." GB";
       @$ext = end(explode('.', $file));
       if ($ext == "jpeg" || $ext == "png" || $ext == "bmp" || $ext == "gif" || $ext == "psd" || $ext == "tif" || $ext == "tiff" || $ext == "tgs"){
           $type = "Image";
           list($width, $height) = getimagesize($file);
           $dimensions = $width."x".$height." px.";
       }
       else if ($ext == "avi" || $ext == "wmv" || $ext == "mp4" || $ext == "mpg" || $ext == "mpeg" || $ext == "mov" || $ext == "wm" || $ext == "ram" || $ext == "swf") $type = "Video";
       else if ($ext == "aac" || $ext == "wav" || $ext == "wma" || $ext == "mp3" || $ext == "aif" || $ext == "mpa") $type = "Music";
       else if ($ext == "doc" || $ext == "docx" || $ext == "pages" || $ext == "txt") $type = "Text/Documents";
       else if ($ext == "asp" || $ext == "php" || $ext == "php3" || $ext == "htm" || $ext == "html" || $ext == "xhtml" || $ext == "css") $type = "Web files";
       else if ($ext == "rar" || $ext == "zip" || $ext == "7z" || $ext == "bz2") $type = "Archive";
       else $type = "Other";
       echo "Name: $file<br />Size: $size<br />Last modified: $time<br />Type: $type<br />";
       if ($ext == "jpeg" || $ext == "png" || $ext == "bmp" || $ext == "gif" || $ext == "psd" || $ext == "tif" || $ext == "tiff" || $ext == "tgs") echo "Dimensions: $dimensions";
       echo "<p />";
   }
}

files('./');

?>
Image
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: May 2011

Post by jacek »

<?php

header('Content-Type: text/plain');

function new_scandir($dir) {
   $files = array();

   $x = 0;

   foreach ( glob("{$dir}*") as $file ) {
       $files[$x] = array();

       // use of stat function to minimize amount of function calls like fileowner()
       // using this  i could've added a lot more to the array but that would've gotten ridiculous
       // i mean, device number? pointless :p
       $stats = stat($file);

       $files[$x]['path']      = $file;
       $files[$x]['name']      = basename($file);
       $files[$x]['in_dir']    = dirname($file);
       $files[$x]['type']      = filetype($file);

       if ( function_exists('finfo_file') ) {
           if ( phpversion() >= '5.3.0' ) {
               $info = finfo_open(FILEINFO_MIME_TYPE);
               $files[$x]['mime_type'] = finfo_file($info, $file);
           } else {
               $info = finfo_open(FILEINFO_MIME);
               $files[$x]['mime_type'] = finfo_file($info, $file);
           }

           finfo_close($info);
       } else {
           $files[$x]['mime_type'] = mime_content_type($file);
       }

       // doesn't work on windows
       if ( function_exists('posix_getgrgid') ) {
           $info = posix_getgrgid(filegroup($file));
           $files[$x]['group'] = $info['name'];
       } else {
           $files[$x]['group_id'] = filegroup($file);
       }

       // doesn't work on windows
       if ( function_exists('posix_getpwuid') ) {
           $info = posix_getpwuid(fileowner($file));
           $files[$x]['owner'] = $info['name'];
       } else {
           $files[$x]['owner_id'] = $stats['uid'];
       }
       
       $files[$x]['created']   = date('d/m/Y h:i:s', $stats['ctime']);
       $files[$x]['last_mod']  = date('d/m/Y h:i:s', $stats['mtime']);

       if ( is_file($file) ) {
           $files[$x]['size'] = (int) ($stats['size'] / 1024) . 'kB';
       }

       if ( is_dir($file) ) {
           $files[$x]['dir'] = new_scandir($file.'/');
       }

       $x++;
   }
   
   return $files;
}

print_r(new_scandir('./'));

?>
Image
Locked