Use the fopen($file,'w')
(even because you want to write to the file also correct !? ) (if only read, use the "a"):
function lerFicheiro(){
$rec = array();
$file = './receitas.txt';
$fh=fopen($file,'w') or die ('Nao foi possivel abrir o ficheiro!');
$data = fread($fh, filesize($file)) or die ('Nao foi possivel abrir o ficheiro!');
fclose($fh);
$regReceita = explode("\n", $data);
foreach ($regReceita as $item){
$registo=explode(";", $item);
if (count($registo) > 1) {
$rec[] = array('receita' => $registo[0], 'descricao' => $registo[1]);
}
}
return $rec;
}
Options:
itt' - Opens read-only; puts pointer at the beginning of file.
alright - Opens for reading and saving; puts pointer at the beginning of file.
pra w' - Opens for saving only; places the pointer at the beginning of the file and deletes the content already written. If the file does not exist, try to create it.
pra w+' - Opens for reading and writing; places the pointer at the beginning of the file and deletes the content that has already been written. If the file does not exist, try to create it.
entitled ' - Opens the file for writing only; puts the pointer at the end of the file. If the file does not exist, try to create it.
pra a+' - Opens the file for reading and saving; puts the pointer at the end of the file. If the file does not exist, try to create it.
I already tried this here the error that appears to me -Imgur.com/a/7XZvr - Megaluk
Check the file size before the fread()
:
if(filesize($file) > 0)
-
function lerFicheiro(){
$rec = array();
$file = './receitas.txt';
$fh=fopen($file,'w') or die ('Nao foi possivel abrir o ficheiro!');
if(filesize($file) > 0) {
$data = fread($fh, filesize($file)) or die ('Nao foi possivel abrir o ficheiro!');
} else {
echo 'Arquivo vazio';
$data = "";
}
fclose($fh);
$regReceita = explode("\n", $data);
foreach ($regReceita as $item){
$registo=explode(";", $item);
if (count($registo) > 1) {
$rec[] = array('receita' => $registo[0], 'descricao' => $registo[1]);
}
}
return $rec;
}
Megaluk, explain your problem better, if it doesn’t get complicated help you.
– rbz
This code what it does is creates a file only if you save something in the file and if the file is created if there are recipes.txt I can list all recipes but if there is not when I will list the error I want to click on the list and the file created alone
– Megaluk
When you need to add details, click "Edit" on the reply, and include them.
– rbz