canadabeeau Posted December 14, 2009 Share Posted December 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/ Share on other sites More sharing options...
Buddski Posted December 14, 2009 Share Posted December 14, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976914 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976916 Share on other sites More sharing options...
Buddski Posted December 14, 2009 Share Posted December 14, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976919 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 This is going to be quite large and complex isnt it Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976920 Share on other sites More sharing options...
Buddski Posted December 14, 2009 Share Posted December 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976922 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 You might want to take a look at Phing. Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976925 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976927 Share on other sites More sharing options...
Buddski Posted December 14, 2009 Share Posted December 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976936 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976941 Share on other sites More sharing options...
Buddski Posted December 14, 2009 Share Posted December 14, 2009 Good Luck with it all.. If you need anymore help you know where to go Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976942 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976944 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 Did you look at my Phing link at all? No point re-inventing the wheel. Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976954 Share on other sites More sharing options...
Buddski Posted December 14, 2009 Share Posted December 14, 2009 Thorpe has a point. If it covers everything you want to do then use it.. Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976955 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 Thanks for all your help, I am going to have a "play" now Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976970 Share on other sites More sharing options...
ignace Posted December 14, 2009 Share Posted December 14, 2009 @thorpe Phing is a good solution for you and I but out-of-proportion for the OP's needs. Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-976999 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 @thorpe Phing is a good solution for you and I but out-of-proportion for the OP's needs. Thats your opinion. Seems like exactly what the op was asking for to me. Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977001 Share on other sites More sharing options...
ignace Posted December 14, 2009 Share Posted December 14, 2009 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); } } } Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977021 Share on other sites More sharing options...
ignace Posted December 14, 2009 Share Posted December 14, 2009 Thats your opinion. Seems like exactly what the op was asking for to me. He has difficulties writing his own installation script imagine him using Phing to do this job for him... Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977023 Share on other sites More sharing options...
trq Posted December 14, 2009 Share Posted December 14, 2009 Thats your opinion. Seems like exactly what the op was asking for to me. He has difficulties writing his own installation script imagine him using Phing to do this job for him... Pfff. Phing is allot simpler than actually writing / maintaining your own scripts. Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977024 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 ignance I have difficulties in writing code of where I have no idea where to start Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977026 Share on other sites More sharing options...
ignace Posted December 14, 2009 Share Posted December 14, 2009 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); .. } } Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977032 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 I did mark this CLOSED and am in-fact looking at Phing Thank-you for all help recieved THIS POST IS NOW CLOSED UNTIL FURTHER NOTICE Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977033 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 I will be following Buddski's advice so thanks to Buddski Please only add more posts if you have good advice or constructive criticism Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977046 Share on other sites More sharing options...
canadabeeau Posted December 14, 2009 Author Share Posted December 14, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/185077-auto-install/#findComment-977051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.