2
This file is performing its function perfectly, but it is returning some errors and I would like to be able to correct them. I need to know if I have to use the fopen
and the file_get_contents
'Cause I can’t get any.
# /pasta/copy.php
<?php
$original = fopen('/pasta/copy.txt','r+');
$ip = fopen('/pasta/origin.txt','r');
$ip = file_get_contents('/pasta/origin.txt','r');
$copia = fopen('/pasta/ok.txt','w+');
if ($original) {
while(true) {
$linha = fgets($original);
if ($linha==null) break;
if(preg_match("/ASSunto/", $linha)) {
$string .= str_replace("ASSunto", $ip, $linha);
} else {
$string.= $linha; #**Linha 13**
}
}
rewind($copia);
ftruncate($copia, 0);
fwrite($copia, $string);
fclose($original);
fclose($copia);
fclose($ip); #**Linha 21**
}
?>
And the mistakes are as follows:
PHP Notice: Undefined variable: string in /pasta/copy.php on line 13
PHP Warning: fclose() expects parameter 1 to be resource, string given in /pasta/copy.php on line 21
The file copy.txt
.
# /pasta/copy.txt
Assunto para primeira linha
ASSunto
The file origin.txt
.
# /pasta/origin.txt
Assunto para segunda linha
The file ok.txt
gets like this.
# /pasta/ok.txt
Assunto para primeira linha
Assunto para segunda linha
The 4 and 5 line do the same thing. The first warning indicates that the variable
$string
not defined, you are using the concatenation operator.=
in a non-initialized variable. Below$copia = ...
declare:$string = "";
. The second warning is generated because of the conflict on lines 4 and 5.– stderr
Removing one of the lines 4 or 5 causes an error to be written to the file
ok.txt
and I don’t understand aboutstring
when making the replacement ceases to function.– Ricardo Jorge Pardal