Skip to content

Instantly share code, notes, and snippets.

View mikedugan's full-sized avatar

Mike Dugan mikedugan

View GitHub Profile
@mikedugan
mikedugan / seasonal-images.php
Created November 7, 2013 14:52
puts up a different image set based on season
<?php
function current_season() {
// Locate the icons
$icons = array(
"spring" => "images_spring/",
"summer" => "images_summer/",
"autumn" => "images_autumn/",
"winter" => "images_winter/"
);
@mikedugan
mikedugan / timer.php
Created November 7, 2013 14:51
script timer thingy
<?php
$execution_time = microtime(); # Start counting
# Your code
$execution_time = microtime() - $execution_time;
$execution_time = sprintf('It took %.5f sec', $execution_time);
?>
@mikedugan
mikedugan / readable-array.php
Created November 7, 2013 14:51
produces readable array
<?php
function readableArray($elements, $delimiter = ', ', $finalDelimiter = ' and ') {
$lastElement = array_pop($elements);
return join($delimiter, $elements) . $finalDelimiter . $lastElement;
}
?>
@mikedugan
mikedugan / pass-gen.php
Created November 7, 2013 14:51
password generator
<?php
function passStr($len, $set = "") {
$gen = "";
for($i=0;$i<$len;$i++) {
$set = str_shuffle($set);
$gen .= $set[0];
}
return $gen;
}
@mikedugan
mikedugan / rename.php
Created November 7, 2013 14:50
rename a file`
<?php
function file_newname($path, $filename){
if ($pos = strrpos($filename, '.')) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
} else {
$name = $filename;
}
$newpath = $path.'/'.$filename;
@mikedugan
mikedugan / multi-upload.php
Created November 7, 2013 14:50
mutliple upload handler
<!-- source: http://www.apphp.com/index.php?snippet=html-multiple-file-input -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input name="upload_files[]" type="file" multiple>
<input type="submit" value="Upload">
</form>
<?php
// PHP code
that shows how to loop through the data as an array
foreach($_FILES["upload_files"]["name"] as $file){
@mikedugan
mikedugan / multi-param.php
Created November 7, 2013 14:49
take arbitrary # of params to function
<?php
// yes, the argument list can be empty
function foo() {
// returns an array of all passed arguments
$args = func_get_args();
foreach ($args as $k => $v) {
echo "arg".($k+1).": $v\n";
}
@mikedugan
mikedugan / login.php
Created November 7, 2013 14:48
a generic login script
<?php
// © 2012-2013, [ Abstract Codify ] Abstractcodify.com All Rights Reserved.
$realm = 'Restricted area';
//user => password
$users = array('admin' => 'mypass', 'guest' => 'guest');
if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
header('HTTP/1.1 401 Unauthorized');
@mikedugan
mikedugan / list-all-files.php
Created November 7, 2013 14:48
lists all files in a dir
<?php
$dir = "./uploads/post-templates";
$files = scandir($dir);
while($files[0] == "." || $files[0] == "..") {
array_shift($files);
}
?>
@mikedugan
mikedugan / list-files.php
Created November 7, 2013 14:47
list certain files by extension
<?php
//Define directory for files listing
//original example
//$files = glob('/path/to/dir/*.xml');
$files = glob('*.php');
//to limit what is displayed you can use a diff listing:
//$files = array_diff($files, array('index.php','opendb.php'));
foreach ($files as $value) {
echo "<a href=http://changetoyoursite/$value>".$value."</a><br>";