File Upload content stored in the same database table?

File Upload content stored in the same database table?

matolamatola Posts: 23Questions: 6Answers: 1
edited April 2016 in Editor

Are there some example(s) of File Upload storing the actual file content (in a MEDIUMBLOB) plus MIME type etc in the same database table? 1

I tried the following, but it did not work well:

        Field::inst( 'sh_fileName' )
            ->upload(
                Upload::inst( $_SERVER['DOCUMENT_ROOT'].'_Z/uploads/__ID__.__EXTN__'  )
                    ->db( 'stakeholders', 'id', array(
                        'sh_fileContent'        => Upload::DB_CONTENT,
                        'sh_fileContentType'    => Upload::DB_MIME_TYPE,
                        'sh_fileName'           => Upload::DB_FILE_NAME,
                        'sh_fileSize'           => Upload::DB_FILE_SIZE
                        )
                     )
            )
            ->setFormatter( 'Format::nullEmpty' )
        )

  1. I am aware it is not the recommended practice... 

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 61,893Questions: 1Answers: 10,144 Site admin

    Hi,

    There aren't any live examples of this for exactly the reason noted in your footnote. However, what you have above should work - is it not writing the content into the database?

    Allan

  • thashimotothashimoto Posts: 5Questions: 1Answers: 0
    edited May 2016

    I have one database table store file name, type, size, just display file name in column also files are stored in separate directory.
    When I add a image file in editor popup, immediately I get an error and it generate new row with file name/size/mime type in same table. Also if row has file uploaded originally(file name is present in column), popup editor doesn't open.
    My php file is almost identical to OP, my js file is same as example js, but I commented out render part.
    Do I need to split table in 2(database+image), and follow your examples?

  • allanallan Posts: 61,893Questions: 1Answers: 10,144 Site admin

    Ah - sorry. I had slightly misunderstood before and hadn't fully realised that you want to keep the content inline in the same table (you did say that, I just missed it - sorry!). That unfortunately it not something that the Editor PHP libraries support at this time. The reason for that is that the file upload input in Editor is async to the rest of the form. The file is uploaded immediately upon selection, thus the row needs to be created in the database at that time (which is the artefact you are seeing).

    To use the upload field type, yes, I'm afraid you would need a separate db table (or store it in the file system - a db table is not required - but it can't be "inline").

    Allan

  • thashimotothashimoto Posts: 5Questions: 1Answers: 0

    Thank you for your time to reply my question.
    The reason I had to put all-in-one DB table was I needed to create a form to report incidents, and allow users to add pictures with single click, and same time it triggers email action to group of people to see the note with attachment. But I think I can split in 2 tables, that way users can have more control with images uploaded and I might go with 2 clicks (upload pic+write to mysql) which don't bother users much, they only use this form 3-5 times a week anyway.
    Thanks again for support!

  • matolamatola Posts: 23Questions: 6Answers: 1

    Thank you for clarifying, Alan.
    It would be really nice to be able to also store file content in the same row. Would you mind have a quick look at your code and see if that could be facilitated? That would be that last piece in my puzzle right now ;-)
    Thanks in advance.

  • allanallan Posts: 61,893Questions: 1Answers: 10,144 Site admin
    Answer ✓

    There is no option for that at the moment I'm afraid. The file is uploaded async to the rest of the form, so it has to be stored somewhere before the main row is created. A left joined table is ideal for that.

    Allan

  • matolamatola Posts: 23Questions: 6Answers: 1

    Thank you. Perhaps there might be some simple example of such left join table for files with you could direct me to?

  • allanallan Posts: 61,893Questions: 1Answers: 10,144 Site admin
    Answer ✓

    The file upload example makes use of a left join for the uploaded image.

    Allan

  • matolamatola Posts: 23Questions: 6Answers: 1

    Thank you. You are really fast. :)

This discussion has been closed.