[RESOLVED] How to copy the file from one folder to another? =]

Hi guys, I am new student in programming and I’m with doubts about the copy of a file for another folder shortly thereafter writing the content during the execution of the file and closed. I made a sort of comparison at the end of the code, and also an attempt to copy, but it is not success. Can you help me?


<?php 

ini_set ( "display_errors", 1 );
ini_set ( "display_startup_erros", 1 );

error_reporting ( E_ALL );

include '../../functions/dataHoraAtual.php';

$address_file = "../../datas/banco.ini";

$bkp_banco_ini = 'bkp/banco.ini';

$erro  = 0;
$msg   = '';
$array = array();


$key_access = isset ( $_REQUEST [ 'key_access' ] ) ? $_REQUEST [ 'key_access' ] : '';

      $host = isset ( $_REQUEST [ 'host'       ] ) ? $_REQUEST [ 'host'       ] : '';

      $port = isset ( $_REQUEST [ 'port'       ] ) ? $_REQUEST [ 'port'       ] : '';

    $dbname = isset ( $_REQUEST [ 'dbname'     ] ) ? $_REQUEST [ 'dbname'     ] : '';

      $user = isset ( $_REQUEST [ 'user'       ] ) ? $_REQUEST [ 'user'       ] : '';

  $password = isset ( $_REQUEST [ 'password'   ] ) ? $_REQUEST [ 'password'   ] : '';


$msg = 'Falta preencher:\n\n';

if ( $key_access == '' ) { $erro++; $msg .= '- Chave de acesso'; }

if (       $host == '' ) { $erro++; $msg .= '- Host';          }

if (       $port == '' ) { $erro++; $msg .= '- Porta';         }

if (     $dbname == '' ) { $erro++; $msg .= '- Base de dados'; }

if (       $user == '' ) { $erro++; $msg .= '- Usuário';       }

if (   $password == '' ) { $erro++; $msg .= '- Senha';         }


if ( ( $erro > 0 ) and ( $msg !== '' ) ) { $array = [ 'cod' => 1, 'erro' => $erro, 'msg' => $msg ]; }

else {

    $rows = parse_ini_file ( $address_file, true );

    if ( @$rows [ $key_access ] )
    {

        unset ( $rows [ $key_access ] );

        $file = fopen ( $address_file , "w" );

        foreach ( $rows as $key_access => $valor )
        {

            $linhakey_access = "[" . $key_access . "]\n";

            fwrite ( $file, $linhakey_access );

            foreach ( $valor as $k => $v )
            {

                $linhaItem = "\t" . $k . "=" . $v . "\n";

                fwrite ( $file, $linhaItem );

            }

        }

        fclose ( $file );

        if ( !copy ( $file, $bkp_banco_ini ) ) { echo "
            
            <script type='text/javascript'>

                alert ( 'Copy failed... =[' );

            </script>"; }

    }

    /*=================================================================================
    ==== Escreve as novas informações no arquivo banco.ini. Se não tiver o arquivo ====
    ====     a função file_put_contents cria o arquivo e insere todos os itens     ====
    =================================================================================*/

    file_put_contents ( $address_file, 

        "[" . $key_access   . "]"       .

        "\r\n\thost = "     . $host     .

        "\r\n\tport = "     . $port     .

        "\r\n\tdbname = "   . $dbname   .

        "\r\n\tuser = "     . $user     .

        "\r\n\tpassword = " . $password .

        "\r\n\n", FILE_APPEND );
    

    $msg = 'Realizado com sucesso.';
    
    $array = [ 'cod' => 0, 'msg' => $msg ];

}

header ( 'Content-Type: application/json' );

echo json_encode ( $array );
type or paste code here

I think you should use the copy function. The usuage is exactly like in Linux except you don’t need sudo.

Get settled here, but thanks. \o/

<?php 

ini_set ( "display_errors", 1 );
ini_set ( "display_startup_erros", 1 );

error_reporting ( E_ALL );

include '../../functions/dataHoraAtual.php';

$address_file = "../../datas/banco.ini";

$bkp_banco_ini = "../../datas/bkp/banco" . dataHoraAtual() . ".ini";

$erro  = 0;
$msg   = '';
$array = array();


$key_access = isset ( $_REQUEST [ 'key_access' ] ) ? $_REQUEST [ 'key_access' ] : '';

      $host = isset ( $_REQUEST [ 'host'       ] ) ? $_REQUEST [ 'host'       ] : '';

      $port = isset ( $_REQUEST [ 'port'       ] ) ? $_REQUEST [ 'port'       ] : '';

    $dbname = isset ( $_REQUEST [ 'dbname'     ] ) ? $_REQUEST [ 'dbname'     ] : '';

      $user = isset ( $_REQUEST [ 'user'       ] ) ? $_REQUEST [ 'user'       ] : '';

  $password = isset ( $_REQUEST [ 'password'   ] ) ? $_REQUEST [ 'password'   ] : '';


$msg = 'Falta preencher:\n\n';

if ( $key_access == '' ) { $erro++; $msg .= '- Chave de acesso'; }

if (       $host == '' ) { $erro++; $msg .= '- Host';          }

if (       $port == '' ) { $erro++; $msg .= '- Porta';         }

if (     $dbname == '' ) { $erro++; $msg .= '- Base de dados'; }

if (       $user == '' ) { $erro++; $msg .= '- Usuário';       }

if (   $password == '' ) { $erro++; $msg .= '- Senha';         }


if ( ( $erro > 0 ) and ( $msg !== '' ) ) { $array = [ 'cod' => 1, 'erro' => $erro, 'msg' => $msg ]; }

else {

    $rows = parse_ini_file ( $address_file, true );

    if ( @$rows [ $key_access ] )
    {

        unset ( $rows [ $key_access ] );

        $file = fopen ( $address_file , "w" );

        foreach ( $rows as $key_access => $valor )
        {

            $linhakey_access = "[" . $key_access . "]\n";

            fwrite ( $file, $linhakey_access );

            foreach ( $valor as $k => $v )
            {

                $linhaItem = "\t" . $k . "=" . $v . "\n";

                fwrite ( $file, $linhaItem );

            }

        }

        fclose ( $file );

    }

    /*=================================================================================
    ==== Escreve as novas informações no arquivo banco.ini. Se não tiver o arquivo ====
    ====     a função file_put_contents cria o arquivo e insere todos os itens     ====
    =================================================================================*/

    file_put_contents ( $address_file, 

        "[" . $key_access   . "]"       .

        "\r\n\thost = "     . $host     .

        "\r\n\tport = "     . $port     .

        "\r\n\tdbname = "   . $dbname   .

        "\r\n\tuser = "     . $user     .

        "\r\n\tpassword = " . $password .

        "\r\n\n", FILE_APPEND );
    

    $msg = 'Realizado com sucesso.';
    
    $array = [ 'cod' => 0, 'msg' => $msg ];

}

    

    $copy_success = copy ( $address_file, $bkp_banco_ini );

    /*============= \o/ resolved \o/ =============*/

    if ( !$copy_success )
    {
        
        $msg = 'Não foi possível gerar um backup.';    
        
        $array = [ 'cod' => 0, 'msg' => $msg ];
        
        echo json_encode ( $array );
        
        die();

    }

    /*============= \o/ resolved \o/ =============*/


header ( 'Content-Type: application/json' );

echo json_encode ( $array );

No problem. Glad you solved it. I also looked at your code closer and you are using the copy function. Guess I missed it when looking through your code.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.