PHP Coding: chmod() my new best friend

phpFor all of you coders out there, I just found a new best friend, its name is chmod().  For those of you familiar with Unix/Linux this command will look familiar.

Background: I was implementing an online store for a customer who already had an existing website, but wanted to be able to sell her goods online (I’m trying to keep things vague on purpose when describing the website).  I built her a custom content management system for her online store that allowed her to upload pictures of her goods and input pricing data.  My code would take the image she uploaded, save it, then copy it and resize the copy to make a thumbnail image.  For whatever reason though, her web host was locking down permissions on the first file.  This became problematic because it doesn’t do any good to have a file that you can’t read…

Solution: After using the move_uploaded_file() command I used the chmod() command to reset the file permissions.

Here is the format of the chmod() function: chmod($filename$mode );

I also ran into a gotcha that I didn’t expect…when specifying the mode, do not specify it as a string, (ie. “755″), instead you specify it as a number (ie. 755), even further though, you have to throw in a leading zero (ie. 0755).

Hopefully, you found this post helpful, happy coding.

Please note that any links to Amazon.com are affiliate links and I do make a small percentage of the purchase price as a commission.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

You can also share this post on your favorite social network using the Share/Save button below.
  • Share/Bookmark

Leave a Reply

Anti-Spam Protection by WP-SpamFree





Two Miles Solutions, LLC
Create your WordPress blog in minutes!

I use HostMonster.com for my hosting and love it. They usually have a special for $5.95/month for hosting plans.

They feature 1-click installers for many popular website scripts like WordPress for blogging.

Click the image below to signup

Get Adobe Flash playerPlugin by wpburn.com wordpress themes