Jump to content

Recommended Posts

Hi I have a directory lets say "apps" that I then have more folders ie "email", "projectmanagment" now each of these "apps" need a mysql table, each of these apps has a .sql or.xml ready to import. Now my question is Moodle has it so you just put the folder in the "app" folder and go to a page and it auto installs, it has already detected the new folder in the apps folder and runs a script to import the .sql or .xml.

My question is how can I do this, I mean the detect automatically part?

 

Oh and as usual thanks in advance, I really do appreciate it

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/
Share on other sites

So basically you want a script to

- Read a directory.

- Find what folders are in it.

- Run SQL queries found inside those folders

 

if this is the case have a read of this

http://php.net/manual/en/function.readdir.php

and take note of the 'See Also' section

 

Let me know if Ive missed anything..

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976914
Share on other sites

Buddski basically this:

1. I add my email app to the "app" folder

2. I visit mydomain.com/appinstall

3. A script detects the email app

4. Script check if xml or sql

5. Script runs import of xml or sql

6. Maybe 10 days later do steps 1 to 5 for a project management app

 

app folder: root/app

email app: root/app/email

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976916
Share on other sites

Yeah.. Look into readdir, file_exists and is_dir

You need to iterate through the APP folder using readdir

For each item found you need to make sure it is a directory using is_dir

If is_dir returns true.. you then need to iterate through that dir (ie. apps/email) and look for your xml or SQL file using file_exists..

 

Hope this helps..

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976919
Share on other sites

Its only as complicated as the install files..

Its the same as all code.. The more you want from it, the more complex it gets..

If all you are doing is entering SQL data its easy, if you have to move files its a little harder etc..

 

Then you may want configuration pages etc..

Its all up to you :P

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976922
Share on other sites

Well its got to be done so I may as well do it sooner rather than later, I would have to make it so it deleted the sql and xml so it would not constantly reinstall it. By the way what you said in the above posts, what  if I randomly decide to make a im app, will the above functions search all folders within "app"? or not?

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976927
Share on other sites

It really depends how you want to do it..

You could create a 'Application Installer' page that looks through the APP folder and lists them with radio buttons or something so you can select which application(s) to install.

 

The only limit is your imagination and your willingness to code a monster :P

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976936
Share on other sites

Its going to be for an opensource project so anyone can make an app, so I need something that will search all folders in the "apps" folder for the .sql or.xml like Moodle does (don't know if u have used it)

 

Thanks for your continued help with all my sagas

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976941
Share on other sites

what do you think of this:

 

function searches for all folders in "app" direcotry

if exists checks for install.sql or install.xml and if tue runs import function using those files

 

Does this sound like its possible or not? The part I am not sure of is the looped if exists and the search all folders in the "app" directory

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976944
Share on other sites

function getMySQLResult($result, $mode = MYSQL_ASSOC) {
    $rows = array();
    while ($row = mysql_fetch_array($result, $mode)) {
        $rows[] = $row;
    }
    return $rows;
}

function getDirectoriesFrom($directory) {
    $directories = array();
    $directory = rtrim($directory, '\/');
    if ($dh =@ opendir($directory)) {
        while (false !== ($file = readdir($dh))) {
            if (!isDot($file) && is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
                $directories[] = $directory . DIRECTORY_SEPARATOR . $file;
            }
        }
        closedir($dh);
    }
    return $directories;
}

function isDot($file) {
     return $file !== '.' && $file !== '..';
}

function installApplication($applicationDirectory) {
    // your logic here
}

// assuming you don't move the directories and the installation directory has the same residence as the applications
$query = 'SELECT directoryname FROM applications';
if ($result = mysql_query($query, $databaseConnection)) {
    $directoryNames = getMySQLResult($result);
    $directories = getDirectoriesFrom($directory);
    foreach ($directories as $applicationDirectory) {
        if (!in_array(basename($applicationDirectory), $directoryNames)) {
            installApplication($applicationDirectory);
        }
    }
}

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977021
Share on other sites

Pfff. Phing is allot simpler than actually writing / maintaining your own scripts.

 

Yes we know ;) Let him first try it the hard way so he will see the advantage of using Phing later on.

 

@rhodry_korb look at my post http://www.phpfreaks.com/forums/index.php/topic,280606.msg1329192.html#msg1329192 All that is left for you to do is provide an implementation for installApplication($applicationDirectory) which could be something like:

 

function installApplication($applicationDirectory) {
    $sqlFile = $applicationDirectory . DIRECTORY_SEPARATOR . 'install.sql';
    if (file_exists($sqlFile)) {
        $sql = readfile($sqlFile);
        ..
    }
}

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977032
Share on other sites

ignace I got your script and did the following

<?php
function getMySQLResult($result, $mode = MYSQL_ASSOC) {
    $rows = array();
    while ($row = mysql_fetch_array($result, $mode)) {
        $rows[] = $row;
    }
    return $rows;
}

function getDirectoriesFrom($directory) {
    $directories = array();
    $directory = rtrim($directory, '\/');
    if ($dh =@ opendir($directory)) {
        while (false !== ($file = readdir($dh))) {
            if (!isDot($file) && is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
                $directories[] = $directory . DIRECTORY_SEPARATOR . $file;
            }
        }
        closedir($dh);
    }
    return $directories;
}

function isDot($file) {
     return $file !== '.' && $file !== '..';
}

echo getDirectoriesFrom('apps');
?>

 

it will just echo "ARRAY"

Have I done something wrong? Can you explain your code more, when you do you will have solved my problem

Link to comment
https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977051
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.