PHP-tip: How to fix problems with file permissions

Have you tried to create or read a file with fopen, file_get_content or file_put_content and only getting permission denied.
A reason for this error message can be that the file you are about to read/write already exists and with your account as owner.
A quick, and safe, fix is to change the owner and group of the file to the same owner/group as the apache-server is running as.
Example:
Let’s say you are about to update index.htm. This file was uploaded by you to the ftp-server, and so the file is owned by foo (you) which is a member of the group bar. The Apache-server how ever is being run by user:group apache. the user:group apache does not have permission to change anything to this file.
So what you do is:

  • Connect to the web server with ssh (shell account)
  • Change ownership to the file to apache by running chmod apache:apache index.htm

Now you should be able to change the content of the file with PHP.
I have seen suggestions on websites where they say that you should change the mode to 775 or even worse, 777. This is a security breach and so you should not do so.
Hope this helps someone out

Suggested links

Virtual Ubuntu

Because of an introduction to laptop at work, I have to bring my laptop back and forth to work. Because this isn’t my laptop I feel that I cannot run Ubuntu Linux as the main OS – although I wish I could. Instead I have to rely heavily on windows 7 and VirtualBox.
As of writing this I have already had two crashes. This could be caused by the fact that the Ubuntu version I am running is quite new, and so the VirtualBox version I am running does not support it fully.
During this process I have tried to find out if it was possible to make the physical Dell D420-laptop virtual using virtualization tools – also known as P2V. VMWare has such a tool, but when you only have VMWare Server installed, making a Linux OS virtual is not possible.
What I ended up doing was to create a Virtual partition. Then install Ubuntu 10.04 (LTS). Then I started moving files and folders from the old laptop to the new one.
I used SCP in this process.
The good thing about doing this by hand is that you have total control over the process. The “drawback” is that it takes time.
I hope that the guys (and girls) at Virtualbox (Oracle) will start developing a P2V with Linux-support for VirtualBox. It would be a nice addition to what seems to be a great product.

No more c# on iPhone

Apple does not stop to surprise me. Luckily I haven’t started writing iPhone/iPad applications, and I am not sure if I want to either. As a Linux / windows user as I see it, I cannot create an application in my development environment. What do I have to do?

  1. Buy a Macintosh ($999 for MacBook)
  2. Get XCode
  3. Register as a iPhone developer ($99 annually

So it’ll only cost me $1100 to start writing applications for iPhone/iPad. That’s quite a lot of applications to sell…

With monotouch you have to pay $399, so that’s not cheap either. And I believe this might be the reason why Mr. Steve “I don’t want to share any money related to Apple products with anyone. And I want total control cos I am a control freak!” Jobs has changed the rules for developing applications for iPhone.

Daring Fireball on why Apple changed Section 331.

Compared to Android:

  1. I don’t have to buy a new computer since I already have one ($0)
  2. I can use Eclipse($0) (which is cross platform, so I can develop in both Windows and Linux environment)
    Or I can use MonoDroid that will ship in August to develop in C#
  3. I have to register as an Android developer ($25)

That’s more than $1000 difference. And even if I had a Mac, the difference is $74.

So, for which Touch phone platform are you going to develop for?

Download direct from ftp-server with php

For a web solution I am currently involved in I had a challenge where files from a FTP-server should be downloaded from the webserver. Thanks to a solution provided by NogDog to cesarcesar @ CodingForums.org in 2007 I managed to solve this.

This is the solution:

First you connect to your ftp-server
$connection = ftp_connect($ftp_server);
$login = ftp_login($connection, $ftp_user_name, $ftp_password);

You then check if it was possible to log in. If it wasn’t, you have to end the script.

if (!$connection || !$login) {
die('Connection attempt failed!');
}

I then created a local directory (/temp/) where I want these files to be downloaded. Then I created a filename.
For my solution I am getting most values from a database, so here I am only creating a example filename

$localfile = "./temp/example.doc";
$remotefile = "/dir/seconddir/example.doc";

Now we do the actual download and at the same time the download to the visitor:

if (ftp_get($connection, $localfile, $remotefile, FTP_BINARY)){
header('Content-Length: '. filesize($localfile));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($localfile).'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($localfile); // send the file
exit; // make sure no extraneous characters get appended
}

The file will be stored locally, and since you don’t know when the user has gotten the file, set up a cronjob to remove downloaded files so that your area doesn’t get filled with files you don’t need.

What you also can do, is to check if the file is downloaded already.

Link to original post
I hope this was of help for someone.

Spec for an iPhone/Ipad application

Over a beer (or two) me and a few friends started discussing all the different and unnecessary iPhone applications available. So we came up with the following idea:

Wife: An iPhone/iPad application that bugs the living …. Out of you. It shall remove/lock any contacts where name resembles a women or a person interested in soccer/football or any other type of sport. It shall also lock applications that can be used to guide the owner to a bar or other establishment selling alcohol. Map-application can be unlocked if spouse is very close by.
Price: $10

Ex-wife: opens any contacts where name resembles a women or a person interested in soccer or any other type of sport. Map also opens for guidance to bards or other establishment selling alcohol.
Price: Wife*2 (everyone knows that x-wifes are expencive) – Wife*200 if mistress is installed… J

Mistress: Same as ex-wife but much more subtle way – it also makes Wife very suspicious and will also force Wife to become more brutal and lock more services…
Price: Wife*100 (everyone knows that mistresses are way more expensive than wifes)

Child-app: Makes the hardware very unstable. Moves keyboard keys and shortcuts around. Also uninstalls apps at random – and of course especially just when you need them.. J
Price: $1 (kids are just fun!)

In-law: A more subtle version of wife and pretty much a pain in the neck!
Price: (hard to say, they aren’t really expensive, just a pain in the neck) $5 (but wife must be installed, so it’s actually $15)

Now all I have to do is find an iPhone/iPad developer.

My experiences with ISPConfig

I’ve installed ISP Config on my virtual server. Mostly for setting up setting up domains that I control. First I installed ISP Config 2, then I found out that the Virtual Server Provider (VSP) had a script for setting up ISP Config 3. So I cleaned the server and set that up in stead.
I learned at least one thing from that change: ISP Config 2 is a much more mature tool than ISP Config 3. I’ve also read that ISPC3 is written from scratch. And based on my experiences I feel that the developers hasn’t used much code or experience from ISPC2. That I think is bad practice.
There could be a logical explanation for this: ISPC3 is created to support more servers, while ISPC2 is for one server setup only. Still there should be code in there that can/could be reused.
Note: I have not checked or asked if ISPC3 is only using new code and not created without looking at the old code.

Good side

But the guys (and girls) behind ISP Config shall hall props for one thing: They have great tutorials for setting up servers and the systems.

Bad side

One thing that ISPC3 really lacks is documentation/manuals. There isn’t any. And there should be. I’ve searched the web to find out if the system creates usernames for e-mail accounts and more. It doesn’t seem like it does. I would also like to change the scheme for creating databases and users. Can’t find documentation for how this is done.
And I have read that there is work being done on documentation – in 2009. This blog was written on April 4, 2010…

Other Tools:

Other tools for managing websites:
Blue Onyx (A fork of BlueQuartz, which again is the open source code that Sun released when they decided to stop developing the Cobalt servers). This is the one that I would have preferred if I had the chance.
Webmin (Although I wouldn’t call this an ISP tool)

If you have other suggestions, drop me a comment.

A short little blog after moving

Recently I moved from one web host to another since the other had Virtual Server Hosting. It now reminds me when I ran a very small hosting company delivering hosting services.
Recent days I’ve have been dealing with a client that has some 2 GB of e-mails that needs to be transfered from his Google e-mail account and to an imap-account.
One solution for moving is to connect to the Google account and then move in an e-mail client. Moving so much data is taking quite some time how ever.
Another solution is using fetchmail. Mbrisby has a nice little blog about using this.

Ubuntu 10.04 – pretties ever

Canonical Ltd. has published screen shots from the upcoming release, 10.04 – The Lucid Lynx. And it looks awesome.

I am really looking forward to installing this LTS version as a clean install and not as an upgrade.
With LL Canonical proves that Linux as an desktop OS can look pretty and for some that is just as important as having the best programs available.

And I think that should be the next step for Canonical Ltd.

After the release of LL the guys and girls at Canonical shall visit the biggest software houses and pinpoint why they are not publishing their most popular products on the Linux platform. When they have gotten their answers, they should start adjusting releases of new versions according to the feedback they’ve gotten.

I believe it is when you have Photoshop, InDesign, Quark, Illustrator, MS Office (not likely to happen), industry standard software for accounting and more available on the Linux platform we can really start feeling the success of this Operating system that Linus Thorvalds of Finland once released.
I hope we get there.

Apple suing mobile maker

On March 2, 2010 Apple decided to filed a lawsuit against the mobile maker HTC. According to their website HTC is infringing on no less than 20 Apple patents.
It’s not the first time Apple files a lawsuit against another mobile manufacturer. In 2009 Apple was taken to court by the Finnish mobile company Nokia and returned that with a lawsuit against Nokia again for infringing Apple patents.
Nothing wrong with that, except that Apple in the first place used standard mobile technology that is patented by Nokia without paying the Finnish mobile maker.

So what have HTC done wrong? Probably by making Nexus One to similar to the iPhone. And when the words iPhone killer + HTC + Google started popping up on mr. Steve Jobs laptop (most likely an Apple), he just could not let this go by.

But there isn’t really anything new in this. In software and hardware development everyone has copied each other. Apple didn’t make the graphical desktop, but they copied it.
Looking forward to see the outcome of this.

Personally I really dislike all these patents and lawsuits. And the Nexus one is far from the same mobile as the iPhone. It’s most likely better as it can multitask and other stuff that the iPhone can’t. Also: You can update it without using the ….. (fill in your favorite swear word here) iTunes that mr Jobs only has made windows and (naturally) apple versions of. And Apple is continuing to make it hard for us Linux users to sync our apple iPods/iPhones with our OS.

The browser choices and their sales pitches

Internet Explorer is the world’s most widely used browser, designed by Microsoft with you in mind. I think that Microsoft wins this “contest”. When you look at it you’ll most likely ask your self:
Would I use the most widely used browser?
And most likely you’ll answer:
Yes I would like to use what everybody else does.

The powerful and easy-to-use Web browser. Try the only browser with Opera Turbo technology, and speed up your Internet connection. So what does this really mean? Opera is providing a powerful and easy-to-use Web browser and with Opera Turbo technology.

I have to ask:
What is hard-to-use in the other browsers? All browsers do more or less the same.
And: What is Opera Turbo technology?
The sales pitch from the Norwegian Browser company does not win me over.


Google Chrome. A fast new browser. Made for everyone. Google uses “Made for everyone” as their sales pitch. And that’s quite close to the Microsoft slogan: “Most widely used” … “…with you in mind”.
That is is a new browser is not that important for me.

Your online security is Firefox’s top priority. Firefox is free, and made to help you get the most out of the web. Mozilla is using safe and secure as the major reason for choosing Firefox as your default browser.

I really don’t think this is a great sales pitch. It does not win me over for sure.

Why not focus on why it is better for you as a web user. Does it render the pages faster than the others? Does it have features that are better for YOU?

Mozilla should, like Google and Microsoft, focus on the user.


Safari for Windows from Apple, the world’s most innovative browser. So this is the most innovative browser. Cool.

But since I am on a Linux machine, I can’t use it. Pitty.

Still, it is the world most innovative browser. Opera, Chrome or Firefox does not use that slogan.

Maybe they should?


You can see the browser choices here