Create PHP automatic file

Asked

Viewed 645 times

1

I have an Administration Panel, and I would like : Every time someone registers, this Dashboard is created automatically (all files).

I’m using the fopen(); but I’m in the 4737 line and not even half of the Panel, has some other method for me to 'Create' these files automatically?! [EDIT]

  • 3

    Fundamental reading: [Ask]. Remember that you know what you want to do, but we only have those 2 paragraphs up there that don’t give the slightest idea of the context. Try to read the link indicated, and then [Edit] the question explaining better what you really need (which files are these, because there are many, which 4737 lines are these, do what exactly, etc.). We are here to help, but for that we need to understand what the situation is.

  • :) Thanks for the indication friend! I made an Dit if you do not understand yet let me know, I think it was clear now.. I just did not understand the negative vote. This disproportions the question and left in an objective way I want to create files automatically without using the metedo of fopen

  • 1

    I’d say he took a step, but we still don’t know what the breeding criteria are. If you want to apply a site "template" to a new client, it would just copy an entire folder with the templates, without any fopen. Create a "/modelo_site/" directory with all the files, and when creating the new user copy all the content to "/past_do_user/" and manage with fopen() only the configuration files, which have passwords etc.

  • It could have a single archive (.zip) with all the essential files of this "admin panel", so just extract everything with each new registration.

  • I thought about this possibility @Danielomine , but I could do it automatically? Ex extract the . zip automatically ?

  • 1

    Yes... PHP zip functions http://php.net/manual/en/book.zip.php

  • @Bacco , I understood the concept I’m new in programming , as I would copy all these files ?! has no password as the own panel comes with the login screen

  • has a code ready here that creates directories and copies all files http://stackoverflow.com/a/2050909/916193

Show 3 more comments

2 answers

6

A solution would be to create a template folder, with everything you need, and copy to the new user folder.

See a code ready, taken from the Stack Overflow in English

<?php 
function recurse_copy($src,$dst) { 
    $dir = opendir($src); 
    @mkdir($dst); 
    while(false !== ( $file = readdir($dir)) ) { 
        if (( $file != '.' ) && ( $file != '..' )) { 
            if ( is_dir($src . '/' . $file) ) { 
                recurse_copy($src . '/' . $file,$dst . '/' . $file); 
            } 
            else { 
                copy($src . '/' . $file,$dst . '/' . $file); 
            } 
        } 
    } 
    closedir($dir); 
} 
?>


If you prefer to extract from a . zip:

Example of the PHP website:

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->extractTo('/my/destination/dir/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>
  • I’ll try the zip I put it in the root of the code ?!

  • in the open you indicate the correct path, in the extractTo() the destination. But I find better the option without zip, gives less maintenance if you are going to exchange a file or other in new versions. Zip is good to save a little space, but it is slower, consumes more cpu (but only when using) and is bad to maintain. Anyway, if you have little disk space the way is zip. in other cases, file.

  • All right I’ll do the tests ! Thank you :D

  • Warning: Ziparchive::extractTo(): Permission denied in /home/ciclumcom/public_html/demo/cadastro/teste/index.php on line 4 ok

  • @Lucasbicalleto in both zip and copied files, you need to set the server folder permissions. There should be a question about this on the site, but if you don’t find a solution, make it separate from this specific problem (because it already escapes the current question). This depends on the operating system used. You need to authorize the webserver user (apache, for example) to write to the folder.

  • Thanks :D I’ll take a look

  • now giving > falied. Code : <?php&#xA;$zip = new ZipArchive;&#xA;if ($zip->open('/public_html/teste_lucas/teste.zip') === TRUE) {&#xA; $zip->extractTo('/public_html/teste_lucas/');&#xA; $zip->close();&#xA; echo 'ok';&#xA;} else {&#xA; echo 'failed';&#xA;}&#xA;?>

  • problem in "open"; see if the path is correct, and if used standard compression (without being the most advanced mode of the new Winzip, which is only compatible with itself).

Show 3 more comments

2

Using the PHP Zip library

function UnzipFile($file_path, $extract_path)
{
    $zip = new ZipArchive;
    $res = $zip->open($file_path);
    if ($res === true) {
        $zip->extractTo($extract_path);
        $zip->close();
        return true;
    } else {
        return false;
    }
}

UnzipFile(
    'c:/caminho/completo/do/arquivo.zip',
    'c:/caminho/completo/do/diretorio/onde/quer/extrair/'
);

With something so simple I could solve.

Just have a zip file containing all the files and folders of the administrative panel system.

When a client signs up, unzip the file to the folder and drop the client. After that, logically, I require you to change some files to customize the installation for the client.

Then at that point you can use a fopen(), file_put_contents() or other more user friendly means with interface for a lay user can fzer the initial setagens.

Using exec()

Alternatively, you can choose the command line.

exec('unzip c:/caminho/completo/do/arquivo.zip c:/caminho/completo/do/diretorio/onde/quer/extrair/');

In Linux environment usually unzip is available.

For Windows environment you can use some specific program like 7zip, Winrar, or even JAVA jar.

The command line is even faster because it uses the resources of the Operating System.



Obs: The method of copying the files recursively is much more time consuming and you still run the risk of something going without copying. But it doesn’t mean it’s wrong and it’s always going to fail.

Browser other questions tagged

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