How to copy a file with PHP?

0

Well my problem is the following wish to print a PHP code in another file.

For example, I have a file x.php , and I have a file y.php , in this file y.php I want it to create a z.php file and make a copy to the x.php file, How can I do it it?

    
asked by anonymous 30.03.2015 / 20:48

3 answers

3

Well, if it is to copy a .php file use the copy () function of php: link

$arquivo = 'arquivo.php';
$copia   = 'copia.php';

if (!copy($arquivo, $copia)) {
    echo "falha ao copiar $arquivo...\n";
}
    
30.03.2015 / 22:53
2

To copy in PHP, as already mentioned, you can use the copy() function that allows you copy a file to another location or to another file because the input name does not have to be the same as the output name.

Example

Content of file x.php

<?php
// Eu sou o arquivo X

echo "X é muita fixe, mas o bubu é mais fixe!";
?>

Content of file y.php

<?php
// Eu sou o arquivo Y

// Vou criar um arquivo "z.php" e copiar para lá o arquivo "x.php"
copy("caminho/para/arquivo/x.php", "caminho/para/arquivo/z.php");
?>

The result of what the y.php file does is create a file named z.php that will contain the contents of the x.php file.

    
30.03.2015 / 23:42
1

You can include PHP codes within another using the include , require and require_once commands. The best in your case is include, which can be used like this:

include('arquivo.php');

Remember that the file to be called must be in the same directory as the current file, if it is in another folder, or in a previous folder, you need to specify exactly the path, examples:

include('scripts/arquivo.php'); // Arquivo em uma pasta que está dentro do diretório atual
include('../arquivo.php'); // Arquivo em um diretório antes

To know the difference between the commands you can give a search. Here is a quick reference: link

    
30.03.2015 / 22:10