Archive for Programming

PHP Programming

28 February 2010

I’ve been working a lot these days to complete several big projects, and a handful of smaller ones. They’re all related to PHP and optimization. I’ve been thinking of a switch-based PHP preloading technique, which I’m trying to implement in a couple of projects.

What I’m trying to do exactly is update my CMSs and make a general purpose one, with modules, plug-ins and as many options as possible exposed to users. Let’s pretend I have a job CMS, let’s say (em)phasis JMS, which is about to get updated pretty soon. While it needs to have all the required options, it should be simple to manage (via source code) and easy to load. All those nice features should be able to load only if requested by user.

The switch-based PHP preloading technique is itself a framework to be placed inside any CMS. I might go as far as providing hooks like those from WordPress and be able to plug them into any kind of framework.

This is currently in a planning stage, with mockups and sketches. I hope they’ll move from the planning board to a sandbox CMS soon.

Photo by Scorpions and Centaurs

HTML 5 And CSS 3 Fever

27 January 2010

While looking at my current theme bugs (Whiskey Air), I decided to take the next step and turn it into a full HTML 5 template.

So, after reading a bit of HTML 5 literature, I started changing the theme, bit by bit. I will release it free after doing all changes. I might even to add it to WordPress themes repository.

  • So, the upcoming tasks should be:
  • Use a framework -  I found 52Framework and I must check it out.
  • Use strict standards.
  • Completely drop IE6 support – If you don’t agree with me on this, the following code should take care of your needs:
#someElement {
 background: red; /* modern browsers */
 *background: green; /* IE 7 and below */
 _background: yellow; /* IE6 exclusively */
}
  • Make the theme compatible with 2.9 – 3.x only – No backwards compatibility for comments and widgets.
  • Add custom functions and widgets.
  • Add documentation.
  • Release.

Image source: SEOGadget.co.uk

Link Of The Week: C Programming

15 December 2009

I’ve been playing lately with the C programming language, as I’m building some modifications to a game engine (it’s a private project, yet).

Cprogramming.com helped me finding some solutions to some common problems (I’m a beginner). Maybe it’ll help you, too.

The California Guys

2 September 2009

The California Guys is a real life story by Allen Pike. It has been described by its author as Some lessons in contracting learned by being a slave programmer.

The web app was rendering using a clutter of frames, which obviously had to go. The visual design was horrible, the usability was horrible, the whole thing was horrible. At first, this excited me because making improvements to something horrible should be easy. It was, at first.

At first glance I thought the variable names were weird, but I wasn’t so fortunate. The variable names were actually in Russian. Ripping out the frames proved out of the question, since they weren’t a design choice, but rather an architecture choice. Soon I was spending a lot of my time fighting against a vortex of tight coupling, magic numbers, presentational HTML, and variable names I couldn’t read.

Read the entire story here and learn how to cope with difficult clients.

9 Methods To Read Files In A Folder Using PHP

22 August 2009

Method 1.

This is the simplest way to list files in a folder:

<?php
$dir = ".";
$dh = opendir($dir);
while (($file = readdir($dh)) !== false) {
echo $file.'<br />';
}
closedir($dh);
?>

In order to add links to the files in the folder, we modify the code as below. Warning, this might pose some serious security threats if publicly exposed.

<?php
$dir = ".";
$dh = opendir($dir);
while (($file = readdir($dh)) !== false) {
echo '<a href="'.$file.'">'.$file.'</a><br />';
}
closedir($dh);
?>

Method 2.

Another way to read the files would be:

<?php
$dir = ".";
$dh = opendir($dir);
$count=0;
while (($file = readdir($dh)) !== false) {
$count++;
if(substr($file,0,1) != ".") {
echo $file.'<br />';
}
}
?>

The line if(substr($file,0,1) != ".") ensures that no .htaccess or .htpasswd file will be read.

Method 3.

The third method of reading and linking files in a directory would be:

<?php
$current=getcwd();
$directory=basename($current);

$dir = '.';
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] =$filename;
}
// let's get rid of the '.' and the '..' parent and ancestor files
$t=array_slice($files,2);
// let's get rid of the index.php file which does the reading
$f=array_search('index.php',$t);
unset($t[$f]);

print('<a href=".">Back</a><br />');
foreach($t as $key => $value) {
echo('<a href="'.$value.'">'.$value."</a><br />");
}
closedir($dh);
?>

Method 4.

The fourth method is the shortest of them all:

<?php
$dir = '.';
$files = scandir($dir);
//print_r($files);

$num = count($files);
for($n=0; $n<$num; $n++) {
echo $files[$n].'<br />';
}
?>

I left print_r($files); in place but commented in order to show the directory array.

Method 5.

This method resembles phpMyAdmin’s way of listing files with an icon for folders and files. Further customization could show all file types, such as images, archives, php files, html files, css files, and so on. Notice how the ls_recursive() function can be nicely tucked away in a functions file.

<?php
function ls_recursive2($dir) {
if(is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file) {
$currentfile = $dir."/".$file;
$last_dir = "";
$count = substr_count($currentfile, '/');
$minus_count = substr_count($_SERVER['DOCUMENT_ROOT'], '/');
$count -= ($minus_count + 2);

for($p = 0; $p<$count; $p++) {
$last_dir .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}

if(is_dir($currentfile)) {
if($file != '.' && $file != '..') {
$last_dir .= "<img src='images/folder.gif' alt='' align='middle' width='16' height='16' border='0'>&nbsp;<a href=\"".$currentfile."\">".substr($currentfile, strrpos($currentfile, '/'))."</a><br />";
echo $last_dir;
ls_recursive2($currentfile);
}
}
else {
$last_dir .= "<img src='images/file.gif' alt='' align='middle' width='16' height='16' border='0'>&nbsp;<a href=\"".$currentfile."\">".substr($currentfile, strrpos($currentfile, '/'))."</a><br />";
echo $last_dir;
}
}
}
}
ls_recursive2('.');
?>

Method 6.

Again a nice function for displaying both files in folders and files outside, a remake of the first method:

<?php
function listFolder($path) {
$dir_handle = opendir($path) or die("Unable to open $path");

$dirname = end(explode("/", $path));

echo ("<li>$dirname\n");
echo "<ul>\n";
while (false !== ($file = readdir($dir_handle))) {
if($file!="." && $file!="..") {
if(is_dir($path."/".$file)) {
listFolder($path."/".$file);
}
else {
echo "<li>$file</li>";
}
}
}
echo "</ul>\n";
echo "</li>\n";

closedir($dir_handle);
}
echo '<ul>';
listFolder('.');
echo '</ul>';
?>

Method 7.

Unlike the methods above, this one counts files of a certain extension (in this case, gifs):

<?php
$d = opendir('.');
$count = 0;
while(($f = readdir($d)) !== false)
if(ereg('.gif$', $f))
++$count;
closedir($d);
print "Files=$count";
?>

Method 8.

This method shows how many files (not folders) are in a directory:

<?php
function num_files($directory) {
    return count(glob($directory."*.*"));
}
echo num_files('');
?>

Method 9.

The ninth method adds an interesting feature, file upload. While reading the files in a folder, the script has the capacity to upload a new file in the current directory.

<?php
if (isset($_POST['submit'])) {
copy($_FILES['file']['tmp_name'], $_FILES['file']['name']);
}

$dir=opendir(".");
$files=array();
while (($file=readdir($dir)) !== false) {
if($file != "." and $file != ".." and $file != "index.php") {
array_push($files, $file);
}
}
closedir($dir);
sort($files);

foreach ($files as $file)
print "<a href='$file'>$file</a><br />";
?>
<form action="<?php echo $PHP_SELF ?>" enctype="multipart/form-data" method="POST">
Add a new file: <input type="file" name="file" />
<input type="submit" name="submit" value="Upload file">
</form>

That’s all!

Notice that all examples use ‘.’, which means the curent directory. By using ‘/’ these functions would scan the parent directory.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes