fopen trouble | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

fopen trouble

I created a class in php to open a file read and write, the write function is working just fine, the problem is to read, i don't have any errors and still don't work, below i've pasted my code, thanks for the help! echo "<!DOCTYPE html> \n<html>\n<head><title>file open</title>\n<link href='' rel='stylesheet' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous'>\n<script src='' integrity='sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa' crossorigin='anonymous'></script>\n</head>\n<body>"; echo "<div class='container'><h1>File Open Example</h1>"; class FileOpenSaver{ private $filePath; private $file; private $fileMode; //class constructor public function __construct($fPath, $mode){ $this->filePath=$fPath; $this->fileMode=$mode; } //class destructor public function __destruct(){ if(! $this->file==null){ echo "Closing File!<br>"; fclose($this->file); echo "File Closed!"; } } //try to open a file public function openFile(){ try { $this->file = fopen($this->filePath, $this->fileMode); }catch (Exception $e){ echo "Captured Exception: ", $e->getMessage(), "<br>"; } } //try to write something in a file public function writeFile($text){ try { fwrite($this->file,$text); }catch (Exception $e){ echo "Captured Exception: ", $e->getMessage(), "<br>"; } } //try to read a file and show in the page public function readaFile(){ try { while(!feof($this->file)){ echo fgets($this->file)."<br>";/

16th Dec 2016, 6:08 PM
Nilton Tadeu Ferreira Filho
Nilton Tadeu Ferreira Filho - avatar
1 Answer
$Text = fgets($this->file); echo $Text; This should work.
27th Dec 2016, 9:39 AM