Archive: Writing Code

Adventures in Debugging CakePHP

The purpose of this blog is to share whatever wisdom I accumulate in my business with other freelance web developers. Normally that means looking back at my previous week and trying to identify what I wish someone had told me at the beginning of the week. I generalize that information so that it will be useful to a wider range of people. With this post, however, I feel the need to be extremely specific.

Read the rest of this entry »

Giving an Uploaded File a Unique Name in PHP

It’s been a while since I’ve done a technical post with code snippets, so I thought I’d share a function that I use at least every week. The majority of my work is with custom CMS or the occasional intranet. In both cases I’m regularly dealing with code where the visitor can upload files to the web site. Maybe it’s pictures or PDFs or something else entirely. In any case, I have a simple function that I use to make sure the filename is unique.

function uniqueName($folderpath,$filename) {
    $adjname = str_replace(” “,”_”,$filename);
    $fulldest = $folderpath.$adjname;
    $originaldest = $fulldest;
    for ($i=1; file_exists($fulldest); $i++) {
        $fulldest = str_replace(”.”,”$i.”,$originaldest);
    }
    return $fulldest;
}

If you tried to upload a file called filename.jpg to a directory where that name already exists, this function would return filename1.jpg.

Flaws: The immediate flaw that jumps out at me when I look at this function is that if someone had a filename with multiple periods, such as my.precious.file.jpg, the function would return my1.precious1.file1.jpg. Since the file names are usually just stored in a database for me, this hasn’t come up as a problem, but it’s something to be aware of.

Any readers our there want to offer their improved versions of this function?

Look Back: Best of Practices

Since I’ve hit the one-year anniversary of being a full-time freelancer, it seems like a fitting time to look back on this blog. Enjoy some of my favorite best practices posts:

Pricing Your Web Design Service
I wrote in this post that I prefer to give clients a fixed price per project, and that’s still true today. I did talk to a lawyer recently who preferred that I charge an hourly rate. That’s the world a lawyer lives in, so that isn’t too surprising. I’ve also worked a few times with a client who prefers me to give 3 different prices for best case, worst case, and expected case. I do that in my quotes, but so far the actual invoice has always been for the expected case.

Read the rest of this entry »

More Impressions of CakePHP

A few weeks ago I wrote my early impressions of CakePHP. In this post I’ll share my thoughts on the mystical “bake” function, SEO-friendly URLs, and intuitive code.

Read the rest of this entry »

Early Impressions of CakePHP

This certainly isn’t a comprehensive review of CakePHP. I have a client that wants to use CakePHP so they can support it internally. I’ve just barely scratched the surface, but the whole point of Cake is to allow you to develop something useful on a very short learning curve. Since it is freely available, I thought I’d share my first outing with this rapid development framework for anyone on the fence about giving it a shot.

Read the rest of this entry »

Adventures in CSS Entomology

Today I came across a CSS bug that I had never seen before. Apparently, in IE6/7 there are circumstances under which certain form elements will inherit the margin of the tag they appear in. In my case, I had a contact form where each element was inside a paragraph tag with a left margin of 85 pixels and a grey background. In Firefox and Opera, the form appeared correctly, but here’s how things looked in IE:

CSS bug

Fortunately there is a thread at Webmaster World that explains the phenomenon pretty well, and has several ways to address the problem. I was able to get all browsers to render the page like this:

CSS bug fixed

I’ve been building sites long enough that it isn’t very often I come across a CSS glitch that I’ve never seen before.

Don’t Reinvent the Wheel

When I stop and think about it, I’m amazed at the number of talented code writers who freely share their work with the rest of the world. There are a lot of generous souls in the web design industry. If you want your web site to have a DHTML menu or a thumbnail gallery or a slideshow, you don’t need to start from scratch. It’s surprising how often someone has already been in your shoes and is willing to share the code they’ve already written

Read the rest of this entry »

Tell Your Small Clients About Standardization

I came across a good article yesterday with the title, The business case for Web standards-based development. I like the break down at the end of the article of all the benefits that come with designing a standards compliant web site.

It got me thinking about the clients I’ve had in the last year. Most of them don’t really know or care that I did my best to adhere to standards when designing their sites, and I think that’s probably typical of most of the clients of freelance designers. In truth that’s probably my fault. I’ve always made the assumption that the advantages of standardization really only apply to larger sites with high profiles and lots of traffic. As I look at this list, though, many of them hold just as much appeal for the “little guy”.

Read the rest of this entry »