P
P
Proshka172015-10-06 20:50:16
PHP
Proshka17, 2015-10-06 20:50:16

Why is the file (php) not being read?

<?php

class Myclass {

public $fullText='';

public function SayHello($text) {
echo 'i,'.__CLASS__.'.I say '.$text.'';
}


/////////////////////////////////
public function ReadFile() {
 if (fopen("readit.txt", "r")) 
 {
 while (!feof(fopen("readIt.txt", "r")))
 {
 $this->fullText .= fgets(fopen("readIt.txt", "r"), 999);
 }
 }
return $this->fullText;
}
/////////////////////////////////


}
$first = new Myclass;
echo $first->ReadFile();



?>

For some reason, it displays a blank page. There is one word in the file. I started learning OOP in php today...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivanq, 2015-10-06
@Proshka17

public function ReadFile() {
    if($f = fopen("readit.txt", "r"))  {
        $this->fullText = fread($f, filesize("readit.txt"));
    }
    fclose($f);
    return $this->fullText;
}

In fread, fgets, feof, you need to supply an already existing object. First you need to write it to a variable. But there is an easier option:
public function ReadFile() {
    return file_get_contents("readit.txt");
}

PS Please don't delete anything extra from my code

D
Denis, 2015-10-06
@prototype_denis

First, read the doc...
php.net/manual/en/function.fgets.php
php.net/manual/en/function.fopen.php
And then write these lines at the beginning of the file

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question