Skip to content

Instantly share code, notes, and snippets.

View davidrautert's full-sized avatar

David Macedo davidrautert

View GitHub Profile
@davidrautert
davidrautert / foldersToArray.php
Created February 1, 2011 19:16
Recursively scan files and folders in a directory, building into either a multidimensional array or flattened array of full paths
function foldersToArray($path, $flat = false) {
$folderArray = array();
if($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if($file != '.' && $file != '..') {
if(!in_array($path.'/'.$file, $folderArray) && is_file($path.'/'.$file)) {
if($flat) {
array_push($folderArray, $path.'/'.$file);
} else {
array_push($folderArray, $file);
@davidrautert
davidrautert / string.prototype.clip.js
Created January 21, 2011 17:18
Reduce a string's length by clipping characters off the end, adding optional characters to the end such as ellipsis
/*
** String prototype for clipping text and optionally adding ellipsis
** Does not modify original variable
** -Example-
** var thisStr = 'Some string of text';
** var newStr = thisStr.clip(10, '...', true); // Some...
** var newStr = thisStr.clip(15, '...', true); // Some string of...
*/
String.prototype.clip = function(len/* Number */, str/* String */, bound/* Boolean */) {
var origLen = len;
@davidrautert
davidrautert / flatten files in folder to array
Created December 1, 2010 17:54
recursive function to find all files in subdirectories and push them into a flat array
$files = array();
function returnFiles($dir) {
global $files;
$dh = opendir($dir);
while(false !== ($filename = readdir($dh))) {
if($filename == '.' || $filename == '..') {
continue;
}
echo $dir.DIRECTORY_SEPARATOR.$filename . "\n";
if(is_file($dir.DIRECTORY_SEPARATOR.$filename)) {
@davidrautert
davidrautert / folder check
Created November 29, 2010 20:53
makes sure the full path we want to work with exists
function checkFolders($path) {
$bits = explode(DIRECTORY_SEPARATOR, $path);
$sandbox = '';
foreach($bits as $bit) {
$sandbox .= $bit . DIRECTORY_SEPARATOR;
if(!is_dir($sandbox)) {
mkdir($sandbox);
}
}