Insert the result of a query into a php file

1

I need to create a .php file with the results that appear accessing another .php file, for example:

I access link and this page results in lists of files on a page. For this I use this code:

<?php
$pasta = 'imagens/';

if(is_dir($pasta))
{
    $diretorio = dir($pasta);

    while(($arquivo = $diretorio->read()) !== false)
    {
        echo ''.$arquivo.'<br />';
    }

    $diretorio->close();
}
else
{
    echo 'A pasta não existe.';
}

? >

I need the result "file1.ini", "file2.ini" ... to be written to an .php file. I tried changing the page code as follows:

<?php
    $filename = 'meuteste.php';
    $pasta = '/xampp/htdocs/';
    if(is_dir($pasta))
    {
        $diretorio = dir($pasta);

        while(($arquivo = $diretorio->read()) !== false)
        {
        int file_put_contents ($filename, echo ''.$arquivo.'</a><br />');
        }
    }
    else
    {
        echo 'A pasta não existe.';
    }
?>

But the command is wrong, I get this message when I open link :

  

Parse error: syntax error, unexpected 'file_put_contents' (T_STRING) in C: \ xampp \ htdocs \ lista.php on line 10

Has anyone ever had something like this? Do you know the solution?

Thanks in advance.

    
asked by anonymous 09.10.2015 / 21:01

1 answer

0

You have two syntax errors on this line, remove the int and the echo from the second argument.

int file_put_contents ($filename, echo ''.$arquivo.'</a><br />');
-^                            -----^

The correct one:

It was also necessary to specify the way the file will be written, if each iteration of the while will overwrite the file or add it to the existing one, this is done iformating the third argument as FILE_APPEND

<?php
    $filename = 'meuteste.php';
    $pasta = '/xampp/htdocs/';
    if(is_dir($pasta)) {
        $diretorio = dir($pasta);
        while(($arquivo = $diretorio->read()) !== false) {
            file_put_contents ($filename, $arquivo. PHP_EOL, FILE_APPEND);
        }
    }

Manual - file_puts_content

    
09.10.2015 / 21:21