The Snippets Thread
08-15-2011, 12:00 PM, (This post was last modified: 08-15-2011, 12:02 PM by MyDigitalpoint.)
#1
The Snippets Thread
Based on my own experience, snippets are a great way to learn PHP and sometimes a useful time-saver even if you know it because you can refer to these chunks of code for a quick integration into your coding.

Let's post small snippets that can be useful to other members whether to learn or add extra functionality to a website or blog, in example, WordPress using the PHP executable plugin.

The following snippet it's a directory listing so people can browse a directory on your site and even download files when you have setup a restriction via htaccess that does not allow directory listing:
[/quote]
Code:
<?php
$sd = '.';
$dir = opendir($sd);
echo '<table cellspacing="2" cellpadding="2">
<tr><td><b>Directory Name</b></td><td><b>Dir Type</b></td><td><b>Last Modified</b></td></td>';
while($item = readdir($dir)){
if( (is_dir($item)) && (substr($item, 0, 1) !='.') ){
$dt = filetype($item);
$lm = date('F j, Y', filemtime($item));
echo '<tr><td><a href="./'.$item.'">'.$item.'</a></td><td>'.$dt.'</td><td>'.$lm.'</td></tr>';
}
}
echo '</table><hr />';
rewinddir($dir);

echo '<table cellspacing="2" cellpadding="2">
<tr><td><b>File Name</b></td><td><b>File Type</b></td><td><b>Size</b></td><td><b>Last Mod.</b></td></tr>';
while($item = readdir($dir)){
if( (is_file($item)) && (substr($item, 0, 1) !='.') ){
$dt = filetype($item);
$lm = date('F j, Y', filemtime($item));
$fs = filesize($item);
echo '<tr><td><a href="./'.$item.'">'.$item.'</a></td><td>'.$dt.'</td><td>'.$fs.' bytes</td><td>'.$lm.'</td></tr>';
}
}
echo '</table>';
closedir($dir);
?>
MyDigitalpoint PRO Freelance Marketplace
Reply


Messages In This Thread
The Snippets Thread - by MyDigitalpoint - 08-15-2011, 12:00 PM
RE: The Snippets Thread - by HiddenKnowledge - 10-02-2011, 11:35 PM
RE: The Snippets Thread - by MyDigitalpoint - 10-20-2011, 08:24 AM
RE: The Snippets Thread - by Zach - 10-20-2011, 09:37 AM
RE: The Snippets Thread - by Arthur - 10-27-2011, 01:40 PM
RE: The Snippets Thread - by Zach - 10-27-2011, 02:42 PM
RE: The Snippets Thread - by Arthur - 10-27-2011, 09:01 PM
RE: The Snippets Thread - by Serial Thrilla - 01-18-2012, 02:33 AM
RE: The Snippets Thread - by Arthur - 04-12-2012, 07:28 AM

Forum Jump: