Delete folder while running update

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
musicscore
Power Poster
Power Poster
Posts: 496
Joined: Wed Jan 25, 2006 11:53 am

Delete folder while running update

Post by musicscore »

Hey ther again.

I'm almost finished versie 0.3 of my module and are change the methode.upgrade.php.
With this php file I want to delete a complete folder (including the files in that folder).
Seaching the internet I found this code :

Code: Select all

	// delete jquery directory
		$dir = "/modules/IwPopupManager/jquery";
		if (!empty($dir)) { 
			array_map('unlink', array_filter((array) array_merge(glob("$dir/*"))));
		}
		rmdir($dir);
So I modified the methode.upgrade.php file and tested the upgrade :
  • Installed versie 0.1
  • Copied versie 0.3 using ftp
  • Did the upgrade in the cmsms console
Everything worked except the deletion of the folder.

Please advise how to delete the folder while upgrading the module.

T.I.A. again.
musicscore
Power Poster
Power Poster
Posts: 496
Joined: Wed Jan 25, 2006 11:53 am

Re: Delete folder while running update

Post by musicscore »

I checked the Gallery module (methode.upgrade.php) and found @unlink command.
Put this code in my methode.upgrade.php without any luck :

Code: Select all

@unlink("/modules/IwPopupManager/jquery/popup.js");
@rmdir('/modules/IwPopupManager/jquery');
Please advise. If this is solved I can upload the new version of my module (version 0.3)

T.I.A.
User avatar
chrisbt
Dev Team Member
Dev Team Member
Posts: 204
Joined: Sun Sep 05, 2010 6:11 am

Re: Delete folder while running update

Post by chrisbt »

This is the code I used in the ECB2 module to remove files & dirs on upgrade:

Code: Select all

$module_path = $this->GetModulePath();
if ( version_compare($oldversion, '1.8') < 0 ) {
    // remove sub dirs
    $dirsToRemove = ['/lib/js/images', '/icons'];
    foreach ($dirsToRemove as $delDir) {
        foreach (glob($module_path.$delDir.'/*.*') as $filename) @unlink($filename);
        @rmdir($module_path.$delDir);
    }
    // individual files to remove
    $filesToRemove = ['/lib/js/mColorPicker.min.js', '/changelog.inc', '/lib/js/jquery-ui-timepicker-addon.js', 
        '/lib/js/colpick.js'];
    foreach ($filesToRemove as $delFile) @unlink($module_path.$delFile);
}
It looks like your issue is that you are not using the full module path.
musicscore
Power Poster
Power Poster
Posts: 496
Joined: Wed Jan 25, 2006 11:53 am

[Solved] Delete folder while running update

Post by musicscore »

Thanks a lot Chris.
That did the job.
Post Reply

Return to “Developers Discussion”