PHP optimization

Corey Edwards tensai at zmonkey.org
Tue Apr 8 16:55:22 MDT 2008


On Tue, 2008-04-08 at 15:35 -0700, joecr at linuxmail.org wrote:
> I've got a PHP script that I use at work all them time.  I know the
> FrontPage Server Extensions are evil incarnate, but I have to support
> them.  cPanel can't seem to make a cleanup script that actually works
> so I wrote my own with some help from someone who knows bash & almost
> nothing about PHP.  I know very little about either.
> 
> The problem is that when you run the script on an account with lots of
> domains with the FrontPage Server Extensions installed on them it can
> cause major problems.  I'm willing to rewrite it into just PHP, Perl,
> or python as those all work on our servers.  The most important parts
> are renaming the .htaccess files, deleting the _folders from the
> FrontPage Server Extensions, & fixing the folder permissions.  The
> last one as best as I can tell is being caused by the normal PEBKAC
> issue.

Your problem is pretty obvious. You're scanning the entire directory
structure *28* times. It's kinda like this. Let's say we were doing the
national census so we send out a letter to every known citizen asking
their age. 300 million letters. Next we want to know if they're male or
femail, so we send them another letter. 300 million more letters. If you
do that 28 times, you've sent 8.4 billion letters.

Same is true of your script. Ideally, you should scan the contents once.
Here's a quick shell script to give you the idea.

        #!/bin/sh
        
        DIR=/home
        
        find "${DIR}" | while read file
        do
        	base=$(basename "${file}")
        	if [ "${base}" -eq "_borders" ]
        	then
        		rm -f "${file}"
        	elsif [ "${base}" -eq "_derived" ]
        	then
        		rm -f "${file}"
        	...
        done
        
Corey






More information about the PLUG mailing list