Results 1 to 9 of 9

Thread: Create a Folder [SOLVED]

  1. #1
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Create a Folder [SOLVED]

    Can someone tell me how to programatically create a folder - and also print a message to say its created - from an on_save callback.

    Code:
    //create realestate folder if it doesn't already exist
    public function generateFolder()
    {	
       if(!file_exists(TL_ROOT . '/realestate'))
       {
    
      //createFolder
      //inform that this folder has been created... messages????????
    						
      }
    
    }
    TAA...ta in advance :D

  2. #2
    User
    Join Date
    08-10-09.
    Posts
    31

    Default Re: Create a Folder

    There is a File, Files and Dir one of these classes has the function to create folders. See http://api.contao.org
    Jaap Jansma

    Walk Wise Webdesign

  3. #3
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Create a Folder

    It actually should be Folder class I think, but I just can't make it happen.
    I can't see why this doesn't work, but it doesn't! Theres no errors, just no result.

    From dca/tl_realestate.php

    Code:
    		'onsubmit_callback' => array
    		(
    			array('tl_realestate', 'generateFolder')
    		)
    Code:
    class tl_realestate extends Backend
    {
    
    	//generate a new folder based on street/name AND id initially and each time the street/name is changed
    	public function generateFolder(DataContainer $dc)
    	{
    	$foldername = 'realestate/' . $dc->activeRecord->accomm_street .'_id_'. $dc->id;
    	$folder = new Folder($foldername);
    	
    	//inform that this folder has been created... messages????????
    	}
    
    ....
    Do I need to import something since it extends backend ?

  4. #4
    User
    Join Date
    08-10-09.
    Posts
    31

    Default Re: Create a Folder

    ehm. The code for creating the folder looks alright. It might be that there is something wrong with you callback function. Or that foldername is not correct.
    Jaap Jansma

    Walk Wise Webdesign

  5. #5
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Create a Folder

    i give up for the evening (i've been at this for hours!)
    Thanks Grapio ( and Tru).... I've tried it on an onload_callback and also just trying to create a Folder called realestate.
    I've also tried importing the Files class, the Folder class.

    Code:
    	
    public function generateFolder(DataContainer $dc)
    	{
    	$this->import('Files');
    	$strDirectory = 'realestate';
    	$arse = new Folder($strDirectory);
    	}
    The Registration Module does it this simply, but not through a callback, and the class extends Module
    Code:
    			$this->import('Files');
    			new Folder($this->reg_homeDir . '/' . $strUserDir);
    Anyway, to tired now to think... cheers

  6. #6
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Create a Folder

    Quote Originally Posted by ramjet
    Code:
    class tl_realestate extends Backend
    {
    
    	//generate a new folder based on street/name AND id initially and each time the street/name is changed
    	public function generateFolder(DataContainer $dc)
    	{
    	$foldername = 'realestate/' . $dc->activeRecord->accomm_street .'_id_'. $dc->id;
    	$folder = new Folder($foldername);
    	}
    
    ....
    please note that $foldername should start with $GLOBALS['TL_CONFIG']['uploadPath'] (e.g. tl_files) if you want to create the folder into the "File Manager"

    so the above code should be

    Code:
    $foldername = $GLOBALS['TL_CONFIG']['uploadPath'].'/'.'realestate/' . $dc->activeRecord->accomm_street .'_id_'. $dc->id;
    hope this helps
    Consulenza Contao CMS https://www.intco.it

  7. #7
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Create a Folder

    Thanks ga.n, perfect.
    Importing 'Files' was not necessary, and it works fine in on_submit callback. :D

    Also for anyone interested it will create all the levels needed - so to create folder3 in folder2 in folder1 in tl_files the first two don't need to already exist.

    Code:
    $foldername = $GLOBALS['TL_CONFIG']['uploadPath'].'/folder1/folder2/folder3';
    Any idea how a message is printed back to the screen "The Folder xxx has been created" ?

  8. #8
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default Re: Create a Folder

    This is also true for a file. If you try to create it with new File() then it also auto-creates all the intermediate folders. If the file exists, it just opens it, otherwise, it creates not only the folder structure but also the file itself.

  9. #9
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default Re: Create a Folder

    Regarding
    Any idea how a message is printed back to the screen "The Folder xxx has been created" ?
    That is done bythe use of
    $_SESSION['TL_CONFIRM'][]

    So my complete onsubmit_callback function is
    Code:
    	public function generateFolder(DataContainer $dc)
    	{
    	$foldername = $GLOBALS['TL_CONFIG']['uploadPath'].'/realestate/' . $dc->activeRecord->accomm_street .'_id_'. $dc->id;
    
    			//create the folder and inform  ONLY if it doesn't already exist	
    			if (!file_exists(TL_ROOT . '/'. $foldername))
    			{
    			$folder = new Folder($foldername);
    			//inform that this folder has been created...
    			$_SESSION['TL_CONFIRM'][] = $GLOBALS['TL_LANG']['tl_realestate']['folder_created'] . $foldername;	
    			}
    
    	}
    which creates a new folder if it doesn't exist already, and informs of this at the top of the page in green with a little tick icon.

    A folder to upload images to has been created in the File Manager: tl_files/realestate/Bishop St_id_22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •