PHP – Chmod-ed to Windows hell and back
March 10, 2009 in PHP
Today has not been a good day.
With various things going wrong, I was please that I had got the file upload code done for a site i’m working on. All I needed to do was make sure that the file had the right permissions and then edit it via GD (via an image editor class i have). Here’s where the trouble started..
I started off by chmoding the file to 777 and then trying to do the resize.. this threw an error with permissions.. so i looked at the file.. Read Only.. strange I thought.
Then I checked the folder and that had a weird green square instead of the checkmark in the checkbox. I tried to remove the read only attribute off of this and hte file.. and it kept coming back.. I went all around hte houses, trying all sorts of techniques .. to no avail.
@auroraeosrose in the PHPWomen IRC channel helped a lot as did Dreis.. (thanks muchly)..
After backing up the files and formatting the drive, to reinstalling the Apache/PHP/MySql stack I was no clearer to the answer as to why the file was always ending up with read only permissions..
Finally it came down to the 1 thing that I had overlooked.. the chmod in the code .. the second parameter was 777. I thought this was correct. Aparrantly not. The second parameter of the chmod function requires an octal code.. thus prefixing the 777 with a 0 (zero) fixed the whole issue.
Boy did i really feel dumb today.
So to wrap up, remember to always pass a FOUR digit code through to chmod or face the annoyance and headache I got today.
I’d strongly suggest you don’t use 777 – or if you are (and releasing the code) , you should make it an easy to configure option.
So many PHP apps will default to using 777 for things, which is a huge mistake. Not all hosts run the same – some will use the apache module, where as others will use CGI – and therfore 770 would make a far better choice.
The ‘other’ ownership issue aside, ’7′ is incorrect for images, you’d want 6 (read,write). Setting ’7′ means execute, and could potentially result in exploitable situations. If in doubt, 664 is probably your best choice.
I was adding to to image resizing code myself today – and double-checked http://php.net/chmod where it makes the same reminder.
RE:Alister It’s where I went in the end and prompted the double check
RE: Ian …. Good Point. 644 should be good enough .. will make the changes and test
Thanks