[TYPO3-dev] Set different directories to upload files in TYPO3 from a single field

Guillermo Oramas oramas.g at gmail.com
Thu Nov 14 07:42:26 CET 2013


I have a field in TCA with type group->file and I need to save the files in different directories depending on the year.

Usually when I need to pos-process any field I use the processDatamap_afterDatabaseOperations hook but it is not working properly with files.

This is what I'm doing.

function processDatamap_afterDatabaseOperations ($status, $table, $id, &$fieldArray, &$reference) { 

    if ($table=='tx_students' && isset($fieldArray['documents'])){
        $path = $_SERVER['DOCUMENT_ROOT'].'/folder/';
        $folder = date('Y').'/';
        $filename = date('Y-m-d').' '.$student['first_name'].' '.$student['last_name'].'.zip';
        $filename = str_replace(' ','_',$filename); 

        if (!file_exists($path.$folder)) {
            mkdir($path.$folder, 0755, true);
            $fh = fopen($path.$folder.'index.html','w+');
            fclose($fh);
        }

        rename($path.$fieldArray['documents'],$path.$folder.$filename);

        $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table,'uid='.$student['uid'],array('documents'=>$folder.$filename));
    }

}
I'm trying to store the filename in the DB with the folder path and that works well the first time I save the form. But the following times I get an error because TYPO3 can't find the file. It happens when I try to delete the file as well clicking in the x.

This is the TCA for that field:

    'documents' => array(
        'exclude' => 1,
        'label' => '',
        'config' => array(
            'type' => 'group',
            'internal_type' => 'file',
            'allowed' => 'zip',
            'max_size' => $GLOBALS['TYPO3_CONF_VARS']['BE']['maxFileSize'],
            'uploadfolder' => 'uploads/incoming',
            'show_thumbs' => 1,
            'size' => 1, 
            'minitems' => 0,
            'maxitems' => 1,
        ),
    ),
I want to have those files organized by year but I don't know how to do this in TYPO3. Is it another hook for doing this? It seems like TYPO3 was removing the part of the path before the slash for certain operations.

I'm working with TYPO3 4.5 LTS. Any suggestions?
I have posted this in Stack Overflow as well, if  the code is too messy here: http://stackoverflow.com/questions/19971107/set-different-directories-to-upload-files-in-typo3-from-a-single-field

Thank you.



More information about the TYPO3-dev mailing list