Eriksrocks Posted July 25, 2010 Share Posted July 25, 2010 Before I get to the question let me explain what I am trying to do. I am working on a project that involves fixing bugs in a certain piece of web software and then distributing the fixes along with a script that will apply the fixes/patches to the user's installation of the software automatically. The core of this script will be the patching code that will take the fixes and apply them to the relevant files. I am looking for opinions as to the best way to patch files using PHP. I was originally thinking of distributing the fixes as diff files. I've found a couple of pure-PHP implementations of the Unix utility patch online, and on *nix environments we could always use the shell execution functions to access patch directly. But then someone else on the project brought up an interesting point: Diff files are based on specific line numbers. It is easily conceivable that somebody wanting to use our patches could have already modified their files in some way, adding some custom code, fixing something else, whatever, and that would throw off the line numbers. I assume that in this case, patch would not work correctly because the line numbers would be different. We could always use simple find and replace functions, but I am wary of just doing a simple "find and replace" because of the possibility of replacing something we don't want to replace and the possibility of overlapping patches. In addition, diff files are easy to make, understand, and distribute. He also said: There are also natural search functions which let you allow a match if there is a 1% difference, or, say, up to X characters are different. I think we would write a custom function which uses a combination of str_replace(), isstr() which tells you the first occurrence of one string inside another, and maybe the natural search functions to make sure a good match is happening. However I was unable to find what he was referring to either in the PHP manual or elsewhere online. So, what do you think is the best way to patch files with PHP, keeping in mind my situation in which files could already be slightly modified? I could really use some help on this one. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/208815-best-way-to-patch-files-using-only-php/ Share on other sites More sharing options...
trq Posted July 25, 2010 Share Posted July 25, 2010 I'm not sure there is any good way of doing this. Patches generally need to be applied to unmodified source. That's why any fixes should always be sent upstream, so they can be maintained buy the developers there. As for modifications, as soon as you start down that path, its up to you to maintain your own work. If you want to be able to apply upstream patches along with your own work then you should likely keep your own work in patches too and apply them both (upstream and yours) to an unmodified source. Quote Link to comment https://forums.phpfreaks.com/topic/208815-best-way-to-patch-files-using-only-php/#findComment-1090900 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.