Problem with fopen

Asked

Viewed 871 times

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.

  • Removing one of the lines 4 or 5 causes an error to be written to the file ok.txt and I don’t understand about string when making the replacement ceases to function.

1 answer

3


PHP Notice: Undefined variable: string in ..

The first error is generated because the variable was not declared string, you are trying to concatenate a value to a non-existent variable.

PHP Warning: fclose() expects Parameter 1 to be Resource, string Given in ..

The second error is generated due to lines 4 and 5, or you use fopen or file_get_contents. Variable is being assigned ip the result of the function file_get_contents and not the file pointer opened by fopen.

If you choose to use the functions file_get_contents and file_put_contents instead of fopen and fwrite, your code can look like this:

<?php

$original = file_get_contents('/pasta/copy.txt');
$ip = file_get_contents('/pasta/origin.txt');
$copia = '/pasta/ok.txt';

if ($original === false){
    echo "Não foi possível ler o arquivo";
    exit;
}

$string = str_replace('ASSunto', $ip, $original);

if (file_put_contents($copia, $string) === false){
    echo "Erro ao gravar no arquivo ". $copia;
} else {
    echo "Operação realizada com sucesso.";
}

?>

It is not necessary to say with preg_match whether a word exists or not in the file, the str_replace is responsible for finding and replacing all the occurrences of this word.

If the file specified in file_put_contents does not exist, it will be created, if it exists, it will be superscripted.

  • Could you give me an example that doesn’t write anything in case of error and that closes the files at the end of used is that this file will run every 2 minutes and I think it will change a little the buffers from the Leave So Many Files Open.

  • 1

    @Rjpserver No need to close the Handle opened by file_get_contents, it reads a file and then closes it automatically. See if with that code works well for you, is practically the same answer script.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.