Use Field value in file upload directory

Use Field value in file upload directory

DarrenRWDarrenRW Posts: 2Questions: 1Answers: 0
edited October 2018 in Free community support

I would like to use a field value 'licence.Reg' as a variable in the upload directory: ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads'.**Field::inst( 'licence.Reg' )**. '/__ID__.__EXTN__' )

or assign 'licence.Reg' as a $variable.

Editor::inst( $db, 'licence' )
    ->fields(
        Field::inst( 'licence.Appendix' ),
        Field::inst( 'licence.Reg' ),
        Field::inst( 'licence.Section_Heading' ),
        Field::inst( 'licence.Criteria' ),
        Field::inst( 'licence.Score' )
            ->options( Options::inst()
                ->table( 'score' )
                ->value( 'id' )
                ->label( 'name' )
            ),
        Field::inst( 'score.name' ),
        Field::inst( 'licence.Comments' ),
        Field::inst( 'licence.Reference' )
    )
    ->join(
        Mjoin::inst( 'files' )
            ->link( 'licence.id', 'audit_files.audit_id' )
            ->link( 'files.id', 'audit_files.file_id' )
            ->fields(
                Field::inst( 'id')
                 ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads'.**Field::inst( 'licence.Reg' )**. '/__ID__.__EXTN__' )
                        ->db( 'files', 'id', array(
                            'filename'    => Upload::DB_FILE_NAME,
                            'filesize'    => Upload::DB_FILE_SIZE,
                            'web_path'    => Upload::DB_WEB_PATH,
                            'system_path' => Upload::DB_SYSTEM_PATH
                        ) )
                        ->validator( Validate::fileSize( 500000, 'Files must be smaller that 500K' ) )
                        ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif', 'pdf' ), "Please upload an image" ) )
                    )
            )
    )
    ->leftJoin( 'score', 'score.id', '=', 'licence.score' )
    ->process( $_POST )
    ->json(); 

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    It is possible using a custom upload action, but there is an underlying issue with that approach: the file upload in Editor is async to the rest of the form. So it would be possible for someone to create a new record and upload a file before they have entered the license.Reg information. You could add a validator for that, but consider what to do if the user enters the license.Reg field, then uploads a file and then changes the license.Reg value. Reference broken.

    I would suggest, if possible, store the files independently of the field values.

    Allan

  • DarrenRWDarrenRW Posts: 2Questions: 1Answers: 0

    Hi Allan, thanks for your reply.

    In this particular use case the field licence.reg would by default have a value assigned to it for the purpose of assigning the directory.

    I have tried the following: ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/'.'licence.Reg'.'/__ID__.__EXTN__' ) which results in the following: /uploads/Array/31.png

    How can I get the relevant value from that array?

    Really appreciate the assistance.

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    I'm not actually sure what that array is - in your code above you have licence.Reg as a literal string, so I don't understand why it would be anything other than licence.Reg.

    As I say, you can't get the values from the rest of the form at the point of the upload. It might not yet have a value or it might change, making any value you did get (which you can't :)) invalid anyway.

    Allan

This discussion has been closed.