Note: The modifications described in this document require working with system files, so be careful and make sure you have backups of any files you change, just in case.
Until recently I ran an online strategy game set in space, which was written in PHP. When I switched, the latest version of Mac OS X was 10.3 (Panther). Searching files in Panther was a lot different, namely in that Spotlight wasn’t around back then.
Take for example, a situation which used to happen quite regularly, I would need to do a search for all PHP files in my web development folder, which contained certain text strings, such as ‘FIX THIS’ and so on. Basically text I had put there so I could come back to later.
In Panther, this worked a treat. If I did a search for “all php files in this folder, which contain the phrase ‘FIX THIS’”, the relevant PHP files would be listed.
When I upgraded to Tiger, I realised that the same search was not working – no PHP files were being listed at all, even ones I knew for sure contained that phrase. I had to do some reading to find out why Spotlight was evidently messing up.
In order to provide the almost instantaneous search function, Spotlight maintains a database of index files, along with their meta data and other things. Spotlight indexes most things, such as the file name, creation and modification dates, file size and so on for all files, but it doesn’t index the content of any files it does not know the type of.
To allow developers to add Spotlight support to their applications, Apple use what are known as ‘mdimporters’. These bits of code tell Spotlight how to behave with file types it doesn’t understand by default.
One of the existing Apple mdimporters it turned out was a plain text importer called ‘RichText.mdimporter’. Since PHP scripts are really just plain text files, surely this importer should be covering them?
Mdimporters, like standard applications, are actually special types of folders containing the required resources. Poking about inside the RichText mdimporter I found there was an ‘Info.plist’ file which seemed to contain various pieces of information, but most importantly it was defining which file types the mdimporter would cover!
So I added another entry to this list, to include PHP files, saved the plist and (after making a backup) replaced the original RichText.mdimporter.
Retrying my search nevertheless showed the same problem, despite the changes I had made. I decided the best way to see if my changes were working was to tell Spotlight to completely rebuild it’s index of my computer. A few Terminal commands later and Spotlight was busy at work doing just that.
Note that it can take Spotlight a while to rebuild a complete index, especially if you have lots of files on your computer.
When Spotlight was finished, I retried my search for the second time, and finally I had success!
So the following steps are necessary for Spotlight to begin indexing your PHP files:
- In the Finder, click the ‘Go’ menu and choose ‘Go to Folder…’.
- Type ‘/System/Library/Spotlight/’ and click ‘Go’.
- Copy and paste the file ‘RichText.mdimporter’ to your desktop to edit.
- Right click on the newly copied file, and choose ‘Show Package Contents’. A folder will appear.
- Double click on ‘Contents’ within this new folder.
- Right-click on ‘Info.plist’ and choose ‘Open With’ and then ‘Other…’.
- Select TextEdit or your text editor of choice.
- Locate the part of the file that looks like this:

- Add the code “public.php-script”, positioned as shown here:

- Save the file and close the ‘Contents’ folder.
- You now need to replace the original ‘RichText.mdimporter’ with your updated version on your desktop. Make sure you backup your original first in case.
- Finally, open up ‘Terminal’ – you can find ‘Terminal’ in ‘Applications’ under ‘Utilities’.
- Type the following command and press enter:
- mdimport -r /System/Library/Spotlight/RichText.mdimporter
After Spotlight completes the operation, you should find that PHP scripts are being indexed properly and can now be searched for by content!
Note: This post was originally created as an article. It has now been recreated as a normal post as part of my rearranging.