note 86864 added to function.file-put-contents

From: Date: Thu, 06 Nov 2008 23:31:46 +0000
Subject: note 86864 added to function.file-put-contents
Groups: php.notes 
Request: Send a blank email to php-notes+get-146265@lists.php.net to get a copy of this message
I wrote this script implementing the file_put_contents() and file_get_contents() functions to be compatible with both php4.* and php 5.*. It is a PHP Command line interface script which searches and replaces a specific word recursively through all files in the supplied directory hierarchy. Usage from a Linux command line: ./scriptname specifieddirectory searchString replaceString #!/usr/bin/php <?php $argc = $_SERVER['argc']; $argv = $_SERVER['argv']; if($argc != 4) { echo "This command replaces a search string with a replacement string\n for the contents of all files in a directory hierachy\n"; echo "command usage: $argv[0] directory searchString replaceString\n"; echo "\n"; exit; } ?><?php if (!function_exists('file_put_contents')) { function file_put_contents($filename, $data) { $f = @fopen($filename, 'w'); if (!$f) { return false; } else { $bytes = fwrite($f, $data); fclose($f); return $bytes; } } } function get_file_contents($filename) /* Returns the contents of file name passed */ { if (!function_exists('file_get_contents')) { $fhandle = fopen($filename, "r"); $fcontents = fread($fhandle, filesize($filename)); fclose($fhandle); } else { $fcontents = file_get_contents($filename); } return $fcontents; } ?><?php function openFileSearchAndReplace($parentDirectory, $searchFor, $replaceWith) { //echo "debug here- line 1a\n"; //echo "$parentDirectory\n"; //echo "$searchFor\n"; //echo "$replaceWith\n"; if ($handle = opendir("$parentDirectory")) { while (false !== ($file = readdir($handle))) { if (($file != "." && $file != "..") && !is_dir($file)) { chdir("$parentDirectory"); //to make sure you are always in right directory // echo "$file\n"; $holdcontents = file_get_contents($file); $holdcontents2 = str_replace($searchFor, $replaceWith, $holdcontents); file_put_contents($file, $holdcontents2); // echo "debug here- line 1\n"; // echo "$file\n"; } if(is_dir($file) && ($file != "." && $file != "..")) { $holdpwd = getcwd(); //echo "holdpwd = $holdpwd \n"; $newdir = "$holdpwd"."/$file"; //echo "newdir = $newdir \n"; //for recursive call openFileSearchAndReplace($newdir, $searchFor, $replaceWith); //echo "debug here- line 2\n"; //echo "$file\n"; } } closedir($handle); } } $parentDirectory2 = $argv[1]; $searchFor2 = $argv[2]; $replaceWith2 = $argv[3]; //Please do not edit below to keep the rights to this script //Free license, if contents below this line is not edited echo "REPLACED\n'$searchFor2' with '$replaceWith2' recursively through directory listed below\nFor all files that current user has write permissions for\nDIRECTORY: '$parentDirectory2'\n"; echo "command written by Kolapo Akande :) all rights reserved :)\n"; $holdpwd = getcwd(); //echo "$holdpwd\n"; chdir($parentDirectory2); openFileSearchAndReplace($parentDirectory2, $searchFor2, $replaceWith2); exit; ?> ---- Server IP: 209.41.74.194 Probable Submitter: 64.47.180.2 ---- Manual Page -- http://www.php.net/manual/en/function.file-put-contents.php Edit -- https://master.php.net/note/edit/86864 Del: integrated -- https://master.php.net/note/delete/86864/integrated Del: useless -- https://master.php.net/note/delete/86864/useless Del: bad code -- https://master.php.net/note/delete/86864/bad+code Del: spam -- https://master.php.net/note/delete/86864/spam Del: non-english -- https://master.php.net/note/delete/86864/non-english Del: in docs -- https://master.php.net/note/delete/86864/in+docs Del: other reasons-- https://master.php.net/note/delete/86864 Reject -- https://master.php.net/note/reject/86864 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146265) next »