First, clarify what "but it doesnt seem to work" means. Report any errors using:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Also, no need to loop, you can use arrays in the str_ireplace:
$haystack = file($file); /* put contents into an array */
$search = "$name, $sname"; /* value to be replaced */
$replace ="replaced, replaced"; /* value doing the replacing can be replaced by new variables*/
$haystack = str_ireplace($search, $replace, $haystack); /* case insensitive replacement */
But one problem is, what if $name and $sname are on different lines? No replacement is made. Maybe just use the file as a string:
$haystack = file_get_contents($file); /* put contents into a string */
$search = "$name, $sname"; /* value to be replaced */
$replace ="replaced, replaced"; /* value doing the replacing can be replaced by new variables*/
$haystack = str_ireplace($search, $replace, $haystack); /* case insensitive replacement */