Create a php file

Asked

Viewed 89 times

0

This code what it does is create a file only. The file receitas.txt I can list all recipes, but if not, when I will list the error.

What I want is to click on the list and if there is no file, create it:

function lerFicheiro(){
    $rec = array();
    $file = './receitas.txt';
    $fh=fopen($file,'r') 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;    
}

Mensagem de erro: Warning: rfread(): Length parameter must be greater than 0 Source: Imgur.com/a/7XZvr

  • Megaluk, explain your problem better, if it doesn’t get complicated help you.

  • 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

  • When you need to add details, click "Edit" on the reply, and include them.

1 answer

0


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;    
    }
  • I’ve already tried this here the error that appears to me -https://imgur.com/a/7XZvr

  • Include in the answer, try again...

  • https://imgur.com/a/7XZvr This means that your file is empty. I added one echo that will print this. But you can put $data = ""

  • @Megaluk avoid deleting the comments if it does not disturb that may be with the same problem.

  • I added in the reply the echo and $data = ""

  • Thanks for the help this all working

  • @Megaluk do not forget to mark as solved, so help the next. ;]

Show 2 more comments

Browser other questions tagged

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