Insert the result of a query into a php file

Asked

Viewed 157 times

1

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

I access http://localhost/.php files and this page results me to lists of existing 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 "arquivo1.ini", "arquivo2.ini"... to be saved in a .php. file I tried to modify 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 opening http://localhost/.php files:

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

Has anyone ever been through something like this? You know the solution?

From now on, thank you.

  • removes that int before the file_put...

  • Good afternoon rray, without the int, the following error appears: > Parse error: syntax error, Unexpected 'echo' (T_ECHO) in C: xampp htdocs list.php on line 10

1 answer

0


There are two syntax errors in this line, remove the int and the echo of the second argument.

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

Right:

Also missing specify the form that will be saved the file, if at each iteration of the while overwrites the file or adds to the existing one, this is done by shaping 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

  • Good afternoon, It almost worked, the only error is that the file 'meuteste.php' is being created with only 1 result. Instead of the file being created with the name of all the files in the folder, it is creating with only 1 result :(

  • @Wesley, updated the reply

  • Good evening, thank you very much. !

Browser other questions tagged

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