Anticipating Clients
Thu, 18 January, 2007 – 1:16 am
I've spent today fixing a site that was creating invalid code from images and PDFs with spaces in the filenames. I have to say that the whole MS Windows invention of allowing filenames with a variety of characters and spaces in has just added to the web developer's job.
Despite explaining to clients that spaces and all these other characters shouldn't be left in filenames before they're uploaded, it just seems to fall on deaf ears. All it takes is one incorrectly done filename to invalidate the code on a website. Not good when you pride yourself on producing valid code for your clients. The more I develop sites to allow clients to upload files, images or documents, the more I'm slowly anticipating that I cannot trust that the Client will listen to what we've told them and am trying to ensure that there are fixes in place to prevent this causing a problem.
For anyone who wants a simple function to clean up filenames, this can easily be done in PHP using:
function cleanURL ($string) {
$string = str_replace(" ", "%20", $string); // replace spaces with %20
$string = str_replace("&", "&", $string); // replace ampisands with correct coding
return $string;
}
This is only a simple function which cleans up the major issues I come across. You could use the urlencode() function too, which would cover more characters such as an apostrophe, however that uses plus signs to replace spaces and I'm not sure how that would work in the browser (I'll try it out one day!).
These little methods to second guess your clients will help keep your code valid and also have more chance of preventing site breaks.


One Response to “Anticipating Clients”
For anyone who doesn't understand the use of functions in PHP, simply add the code above in your page (or a global functions file) and then reference it with use in cleaning up a filename eg.
echo "http://www.domain.com/images/".cleanURL($filename);if $filename contained something like "Bob & Jane.jpg" after this function it would become "Bob%20&%20Jane.jpg".
By Sarah on Jan 18, 2007