Questão 44 · TRT-7 2017 · Analista · Tecnologia da Informação

O PHP5 permite manipular arquivos externos, como, por exemplo, arquivos txt, assim como permite realizar tratamento de erro. A seguir, são apresentados o código em PHP5 que abre o arquivo hello.txt e um possível erro associado.

1 <?php
2 $file=fopen("hello.txt","r");
3 ?>

Warning: fopen(hello.txt) [function.fopen]: failed to open stream:
No such file or directory in xpto.php on line 2

Assinale a opção que apresenta o código correto para prevenir o erro em questão.
  1. A)<?php
    if(feof("hello.txt")) {
    echo("Arquivo não existe!");
    } else {
    $file=fopen("hello.txt","r");
    }
    ?>
  2. B)<?php
    if(error().fopen("hello.txt")) {
    exit("Arquivo não existe!");
    } else {
    $file=fopen("hello.txt","r");
    }
    ?>
  3. C)<?php
    if(error_log("Warning: fopen(hello.txt)")) {
    curl_error ("Arquivo não existe!");
    } else {
    $file=fopen("hello.txt","r");
    }
    ?>
  4. D)<?php
    if(!file_exists("hello.txt")) {
    die("Arquivo não existe!");
    } else {
    $file=fopen("hello.txt","r");
    }
    ?>
Responder e ver o gabarito comentado →

Grátis, sem cartão. No Chip você responde, vê o gabarito e o comentário com a pegadinha, o macete e o fundamento na lei.