Jun
30th
2009

.Net Code Snippet
Obviously, it has been a while since I have posted on my blog. Quite simply after starting my full time employment contract I have not had time. Why? I am working long hours doing many new things – such as programming in Coldfusion and .Net.
I took on a position of web developer and have now setup 2 Linux servers, coded a C# .Net app from the ground up and am looking at major site updates using Coldfusion. While entertaining, fun and challenging, it has meant a bit longer days at work than I had anticipated – however I am NOT complaining in the least amount. I have a full time job and am paying bills and getting caught up on things. That’s always good!
The past week and a half has brought more changes – I was told we are going to look at revamping the entire site and work and will be doing it in VB.Net, so more learning, which is one of the things I love most about IT and web development – the constant learning, expanding and maintenance of knowledge. With that, we are looking at making it easier to get content out, expanding on and integrating more with social media and getting an infrastructure in place that will allow forward motion, expansion and easier maintenance. This also means a more enhanced visitor and site user experience, which is always something that is highly important.
The other thing of note, is that I will try my best as I go through Coldfusion and .Net programming to get some tutorials, how-to’s and other related posts up on those subjects. I will be learning quite rapidly and am sure to have some struggles to fight through, and those are the items I would like to post about.
Apr
16th
2009
Working on some freelance projects and personal projects recently, I found myself needing to do some simple things in PHP that actually had some great value in the end. I share these now in hopes that they will help someone else.
- Adjusting Case Sensitivity – You can use the following code to take any string and perform an initial caps on it. The outer function ucfirst performs the first letter capitalization, while the strtolower will automatically convert the entire string to lower case first. This allow any string with mixed case to be converted to initial caps on the first word.
Quick Code
<?php
$myString = "HellO TheRE";
echo ucfirst(strtolower($value));
?>
- More Case Sensitivity – instead of using just ucfirst, why not take a look at ucwords. This nifty little built-in PHP function will take and perform an initial cap on every word of a given string. Again, we would probably want to run the string through strtolower to ensure only the initial caps was performed. This may cause issues, however, if you are converting strings that contain acronyms or other words that are generally in all caps.
Quick Code
<?php
$myString = "cool PHP";
echo ucfirst($myString);
?>
The output will be: Cool PHP
However using it this way, you can see how it would change the output:
Quick Code
<?php
$myString = "cool PHP";
echo ucfirst(strtolower($myString));
?>
The out of that last one will become Cool Php.
- File Uploads – When you allow file uploads, say for web site owners to upload images, it is best to check to see if the file exists first and if so remove the old version first. In this short example a filename is being dynamically built, but that is another discussion in another post.
First, we build our file name – the second thing we do, since we are renaming and files to a standard naming convention is to check to see if the file exists. In this case we used a unique ID, an underscore and the image file name being passed into this function. You can use what ever you need to name your files. Second, the if statement checks for the existence of the file, and if true it deletes it using the PHP unlink function, then copies the new file over and finally reports any errors. If the file does not exist, the function simply moves to the else block and copies the new file.
Quick Code
$fileName = "images/".$id."_".$image;
if(file_exists($fileName))
{
unlink($fileName);
$copied = move_uploaded_file($_FILES['form_element_name]['tmp_name'], $fileName);
if(!copied)
{
echo "upload failed for image ".$fileName." or you didn't upload that file <br />";
}
}
else
{
$copied = move_uploaded_file($_FILES['form_element_name']['tmp_name'], $fileName);
if(!copied)
{
echo "upload failed for image ".$fileName." or you didn't upload that file <br />";
}
}