Free tech support / small logo


Can I bulk fix PHP require/include references?

A pal of mine asks: "Ugh.. I'm facing server-move Hades.. am moving my site dadsrights.org to a hosted solution, and I have dozens and dozens of files with PHP require_once() includes like "/www/dadsrights/XX". They all need to be changed to "/nfs/www/dadsrights.org/XX". Help!!"

Dave's Answer:

Annie, I am sure we can solve this by having you log in to their Unix server and running a simple script. This is, in fact, a perfect job for the Unix stream editor sed, which accepts patterns of the form s/old/new/g to replace all occurrences of "old" with "new". (the trailing 'g' applies it globally, meaning more than once in a single line, if applicable)

But before we do this, it's always smart to back up all the files we're about to tweak. Just in case. :-) To do that, run this command:

$ tar czvf php-file-backup.tgz ./*.php
This will create a new file called "php-file-backup.tgz" that's a compressed 'tar' archive of all the files. If something goes south and things don't work, you can always unpack and restore all the PHP files with tar xzf php-file-backup.tgz).

Now, ready for some shell script programming? Open up an editor (vi, pico, emacs, whatever) with the new name "fixphp.sh", and type in the following:

#!/bin/sh

cd the directory that contains the files you want to fix

for fixme in *php
do
  sed 's|/www/dadsrights/|/nfs/www/dadsrights.org/|g' $fixme > new
  mv new $fixme
  echo fixed $fixme
done

exit 0
That's it. Now, save this out and execute it by feeding it to a subshell:
$ sh fixphp.sh
You will want to make sure that you've specified the correct directory in the script as you've typed it in, of course.

Once you run the script, you should see a list of 'fixed xx, fixed yy' and when it completes, all your PHP include_once() directives should be working fine.

Good luck!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

Edit-in-place to the rescue!

perl -i -p -e 's/\/www\/dadsrights\//\/nfs\/www\/dadsrights.org\//g' *.php

Change the -i to -i.bak if you want foo.php to be saved as foo.php.bak before the changes ar emade

Posted by: Sean at November 17, 2004 3:28 AM

Right idea, but let's try for readability. ;^)

perl -pi~ -e 's{/www/dadsrights/}{/nfs/www/dadsrights.org/}g' *.php

Posted by: Brent Dax at November 25, 2004 8:48 AM

How about for a whole directory tree?

Posted by: John at January 7, 2005 12:36 AM

for a whole directory tree, I'd use the find command probably something like

for fixme in $(find . -type f -name "*.php")

which should match all PHP files in the current directory and all subdirectories too.

Posted by: Dave Taylor at January 7, 2005 1:40 AM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.