A Simple PHP Directory Script

June 14th, 2008

I have a tendency to create a lot of files sometimes and forget what I have named them. Then, I get super annoyed when I have to stop and look them up because the environment I’m working in doesn’t have a built-in directory feature enabled. So, one day in an effort to combat this annoyance, I decided to slap together this simple PHP script that grabs all files from a given directory of a specified file type(s) and turns them into a nice little on screen directory. Here it is:


<?php
$pagetitle = "Directory" ;
$dir = "./" ;
$okfiletypes = array(".php") ;

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n" ;
echo "<html>\n" ;
echo "<head>\n" ;
echo "<title>$pagetitle</title>\n" ;
echo "</head>\n" ;
echo "<body>\n" ;
echo "<h1>$pagetitle</h1>\n\n" ;
$files = array() ;
if (is_dir($dir)) {
  if ($dh = opendir($dir)) {
    while (($file = readdir($dh)) !== false) {
      if (($file != ".") && ($file != "..")) {
        if (in_array(strtolower(substr($file,-4)),$okfiletypes)) {
          $files[] = $file ;
          }
        }
      }
    closedir($dh);
    }
  }
sort($files);
foreach ($files as $file) {
  echo "<div><a href=\"$file\">$file</a></div>" ;
  }
echo "</body>\n" ;
echo "</html>\n" ;
?>

PHP Script to Get Array of FTP Directories and Subdirectories

May 3rd, 2008

I was looking through my web host’s documentation the other day trying to figure out if there was a way to modify my php.ini file even though I’m on a shared hosting package. I was guessing not, but thought it would be worth looking into. I found good news and bad news — I couldn’t actually update the php.ini file, but I could place partial php.ini script in each directory and it would apply the specified changes. I’m trying to disable the on screen error reporting for security reasons. So, yes, it is possible (good news) but I’d have to upload a mini php.ini file into every single directory on my sites (bad news — I have hundreds of folders so that would involve a lot more busy work than I’m interested in.) So, I decided to outsmart the problem…

I created a script using PHP’s FTP functions that goes through and uploads the necessary file into each directory. I set up a cronjob that runs this script every week (in case I forget the file when adding new directories, accidentally delete one, etc.) The code that I’m including here is the first part of this file. This code grabs a list of all directories and subdirectories underneath the root FTP directory and stores them in an array ($dir). The $depth variable controls how many levels deep the loop will go through looking for further directories. However, it will stop if it finds that no new directories have been added on any given loop so you can set the depth to an outlandishly high amount if you’d like.


<?php
$ftp_user_name = "username" ;
$ftp_user_pass = "password" ;
$ftp_server = "ftp.example.com" ;
$depth = 10;
$conn_id = ftp_connect($ftp_server) or (die("Couldn't connect to $ftp_server"));
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (!$login_result) { die("Couldn't log in to FTP account."); }

$dir = array(".");
$a = count($dir);
$i = 0;
while (($a != $b) && ($i < $depth)) {
  $i++;
  $a = count($dir) ;
  foreach ($dir as $d) {
    $ftp_dir = $d."/" ;
    $newdir = ftp_nlist($conn_id, $ftp_dir);
    foreach ($newdir as $key => $x) {
      if ((strpos($x,".")) || (strpos($x,".") === 0)) { unset($newdir[$key]); }
      elseif (!in_array($x,$dir)) { $dir[] = $x ; }
      }
    }
  $b = count($dir) ;
  }

print_r($dir) ;

ftp_close($conn_id);
?>

U.S. State Abbreviations in MySQL Table

April 24th, 2008

I was working on a lead delivery system today and the client needed the states field to be exported as abrevations rather than full names. I had to go to the USPS website, get the list, match up the abbreviations with the names, etc. It was a hassle. Hopefully, my putting this code to create a MySQL table of all 50 United States (and Washington, D.C.) up here, I’ll save some of you a few minutes of monotony. Enjoy!

CREATE TABLE `states` (
`name` VARCHAR( 20 ) NOT NULL ,
`abv` CHAR( 2 ) NOT NULL ,
PRIMARY KEY ( `abv` )
)
;

INSERT INTO states VALUES ('Alabama','AL');
INSERT INTO states VALUES ('Alaska','AK');
INSERT INTO states VALUES ('Arizona','AZ');
INSERT INTO states VALUES ('Arkansas','AR');
INSERT INTO states VALUES ('California','CA');
INSERT INTO states VALUES ('Colorado','CO');
INSERT INTO states VALUES ('Connecticut','CT');
INSERT INTO states VALUES ('Delaware','DE');
INSERT INTO states VALUES ('District of Columbia','DC');
INSERT INTO states VALUES ('Florida','FL');
INSERT INTO states VALUES ('Georgia','GA');
INSERT INTO states VALUES ('Hawaii','HI');
INSERT INTO states VALUES ('Idaho','ID');
INSERT INTO states VALUES ('Illinois','IL');
INSERT INTO states VALUES ('Indiana','IN');
INSERT INTO states VALUES ('Iowa','IA');
INSERT INTO states VALUES ('Kansas','KS');
INSERT INTO states VALUES ('Kentucky','KY');
INSERT INTO states VALUES ('Louisiana','LA');
INSERT INTO states VALUES ('Maine','ME');
INSERT INTO states VALUES ('Maryland','MD');
INSERT INTO states VALUES ('Massachusetts','MA');
INSERT INTO states VALUES ('Michigan','MI');
INSERT INTO states VALUES ('Minnesota','MN');
INSERT INTO states VALUES ('Mississippi','MS');
INSERT INTO states VALUES ('Missouri','MO');
INSERT INTO states VALUES ('Montana','MT');
INSERT INTO states VALUES ('Nebraska','NE');
INSERT INTO states VALUES ('Nevada','NV');
INSERT INTO states VALUES ('New Hampshire','NH');
INSERT INTO states VALUES ('New Jersey','NJ');
INSERT INTO states VALUES ('New Mexico','NM');
INSERT INTO states VALUES ('New York','NY');
INSERT INTO states VALUES ('North Carolina','NC');
INSERT INTO states VALUES ('North Dakota','ND');
INSERT INTO states VALUES ('Ohio','OH');
INSERT INTO states VALUES ('Oklahoma','OK');
INSERT INTO states VALUES ('Oregon','OR');
INSERT INTO states VALUES ('Pennsylvania','PA');
INSERT INTO states VALUES ('Rhode Island','RI');
INSERT INTO states VALUES ('South Carolina','SC');
INSERT INTO states VALUES ('South Dakota','SD');
INSERT INTO states VALUES ('Tennessee','TN');
INSERT INTO states VALUES ('Texas','TX');
INSERT INTO states VALUES ('Utah','UT');
INSERT INTO states VALUES ('Vermont','VT');
INSERT INTO states VALUES ('Virginia','VA');
INSERT INTO states VALUES ('Washington','WA');
INSERT INTO states VALUES ('West Virginia','WV');
INSERT INTO states VALUES ('Wisconsin','WI');
INSERT INTO states VALUES ('Wyoming','WY');

Code to Display a MySQL Table in HTML

April 21st, 2008

Okay, so it’s been almost a month since I last found time to write a post up here. That’s clearly not enough to keep this going as an active, useful blog. So, I’m reevaluating my approach a bit. Unfortunately, I just don’t have a ton of extra time laying around at the moment, so instead of trying to find something that’s just not there, I am going to keep these posts a little more brief and focused.

To start this new tradition, I thought I’d share a snippet of PHP code that I wrote today while setting up a new MySQL database. It grabs all the available data from any given table on your database and exports it to HTML. All you have to change is the tablename variable. Here it is:

$tablename = "exampletable" ;

echo "<h2>$tablename</h2>" ;

$cols = array() ;
$resultcols = mysql_query("SHOW COLUMNS FROM $tablename") or die(mysql_error());
$numrowscols = mysql_num_rows($resultcols) ;
$c = 0;
while ($c < $numrowscols) {
$c++;
$rowcols = mysql_fetch_array($resultcols);
$cols[] = $rowcols[0] ;
}

echo "<table>" ;
echo "<tr>" ;
foreach ($cols as $col) {
echo "<th>" ;
echo $col ;
echo "</th>" ;
}
echo "</tr>" ;
$result = mysql_query("SELECT * FROM $tablename ORDER BY id") or die(mysql_error());
$numrows = mysql_num_rows($result);
$i = 0;
while ($i < $numrows) {
$i++;
$row = mysql_fetch_array($result);
echo "<tr>" ;
foreach ($cols as $col) {
echo "<td>" ;
echo $row[$col];
echo "</td>" ;
}
echo "</tr>" ;
}
echo "</table>" ;

Hope this helps. Let me know if you have questions.

How To Set Up Your Own Blog

March 25th, 2008

It’s hard to believe that ten days have already passed since I posted my first (and, until now, only) entry up here on the NYC Web Design blog. I had intended to be back in a couple days with this entry on how to set up a blog, but my crazy life and various other projects got in the way of that. Well, as they say, better late than never. So, without further ado, here is my advice on how to set up a blog.

Okay, so there’s basically three different ways that you can launch a blog. I’m going to discuss each briefly and then list the advantages and disadvantages that come with each.

  1. Sign up with a blog hosting site such as Blogger, WordPress.com, Blog.com, or LiveJournal. The big advantage here is simplicity — you don’t have to do any actual coding. Instead, you just come up with a title, pick a theme, and all of the sudden, you have yourself a blog. For anyone that’s just blogging recreationally and doesn’t have a strong tech background, this is the way to go. Another plus is that you have built-in exposure for your blog right away because all of the major blog hosts have internal directories that deliver traffic. There are, however, some serious downsides to using one of these hosts: The biggest being a lack of customization ability. Say you want to incorporate your blog into the overall theme of your site? Too bad. Also, these hosts have to cover their costs and so they are going to either charge you directly for premium ad-free service or they are going to stick advertising on your blog. Finally, having your blog hosted on an outside blog site seems pretty unprofessional. If you have a website, you should really incorporate your blog into that site instead of sending users out to an external host.
  2. On the other end of the spectrum, you can build the whole thing from scratch. Sit down with your favorite code editor and a big cup of coffee and reinvent the wheel. I have personal experience with this and can say with authority that it is not fun. After being frustrated by the lack of customization available to me on a blog hosting site, I decided to just write my own blog code. How hard could it be? Actually, there’s nothing too terribly complex about it. You just need a decent knowledge of PHP and MySQL (or equivalent languages) and an understanding of how to create an RSS feed. (If you’re interested, this article is a good place to start.) The problem isn’t the technical complexity involved but the sheer volume of code you have to write to get everything going. Maybe a week or so into the project, one of my colleagues talked some since into me and convinced me that I was crazy for trying to rewrite this. After all, other people have already done the work and are willing to share it for free, so why not take advantage? This brings us to the not too hot, not too cold, but just right #3…
  3. The best way to get a professional, customizable blog started is to download pre-built blogging software such as WordPress (the software powering this blog), b2Evolution, MoveableType, or Textpattern. The advantage here is that you can put as much or as little time and effort into customizing your blog as you want. For example, on this blog, I just started with someone else’s theme and made a few modifications, getting the whole thing up and running in a little under an hour. But, for my personal blog, I spent several hours rewriting both the CSS and the template PHP files to more completely integrate the blog into the rest of the site. Most of these software packages allow you to import themes from other designers so you don’t have to have a ton of technical knowledge to follow this approach. You do, however, need some. Of course, if you’re wanting to get a good blog set up but don’t have the time or skills to do it yourself, you could always hire a web development company to help you out. I think I may know of one…

I had originally intended to include a discussion of blog promotion in this article, but this is already longer than I anticipated. So, we’ll save that for next time. Please feel free to leave comments. Did I miss anything? Did this article help anyone? Are you excited for the upcoming article on blog marketing?

Introduction

March 15th, 2008

Welcome to the NYC Web Design blog! I decided to start this blog because I wanted a place to share all these cool little tips and tricks that I’ve been discovering lately as I wander around the web. You never know how these blogs are going to evolve (that’s part of the fun), but I’m imagining that this blog is going to become a comprehensive source of web design and web development knowledge. In order to achieve this goal, I hope to get lots of community involvement: guest authors, engaging comments, and plenty of feedback.

So what’s this blog going to be about? Well, as you probably gathered from the title, it is going to be about web design (more on the NYC part later). But, that’s not the whole story. I want to talk about web development a good deal too. Before moving on, let me clarify what I mean by the two phrases since they’re often confused with each other.

By web design, I mean the way that web pages actually look on the screen. I like to think of the web as my own canvas. Painters, photographers, sculptors all have their own mediums. Mine, like other web designers’, is the Internet. Specific topics in the web design half of things are going to include CSS, HTML, Graphic Design, Color, Font, Layout, Styles, and anything else that comes up along the way.

In contrast, web development is what gives the design substance. Web developers move beyond the realm of traditional artists because their medium has the ability to instantaneously update itself as new information becomes available. Imagine someone standing in front of the Mona Lisa and saying they wished it was blue and then suddenly the painting turned blue. Web development can make that happen. Of course, many of the changes are much more useful than simply updating colors. As developers, we can deliver customized content to specific users based on their login information, provide instant content updates, allow for interactivity, store and analyze data, and more. Topics in the web development half of this blog are going to include PHP, MySQL, JavaScript, Email Systems, AJAX, APIs, and (again) anything else that comes up along the way.

A truly great website is born when development and design come together. That relationship is what this blog is all about. You’ll find a lot of designers out there who fail to utilize the powerful dynamic elements of the Internet and, on the other side, developers who generate good stuff that gets ignored because it looks horrible. This blog exists to share with you interesting ideas and techniques in both design and development so that you can create truly great websites of your own.

Of course, none of that matters if no one is visiting your site. So, we’re going to talk a bit about marketing too. Consider that the third half. Wait… does that work? Regardless, the Marketing section is going to cover topics such as Search Engine Optimization (SEO), Social Networking, Paid Advertising, Blogging, and (last time, I promise) anything else that comes up along the way.

Well, now that you know what this blog is going to be about, let me take a quick step back explain the title. At first glance, it may seem like a simple SEO stunt. But, I assure you, it’s not. I’m the owner of a Manhattan-based web design/development company called Lantenengo Industries. I like to imagine myself as a modern day Howard Roark (the protagonist of Ayn Rand’s legendary novel The Fountainhead), living here in New York City and refusing to compromise my design style even though it doing so can make my financial situation a little “interesting” here and there. There you go, the reasons for the title. …okay, so maybe I was thinking a bit about Google too when choosing it. Don’t hold that against me.

That’s all for now. Check back soon for the first non-intro post to the new NYC Web Design blog. Better yet, subscribe to the feed so that you’ll always be the first one in the know!