Upload / Filter the files from the metadata tables

Upload / Filter the files from the metadata tables

Melissa89Melissa89 Posts: 1Questions: 0Answers: 0

Good morning,
I have an issue with upload class in PHP.
I am using it to upload images for a web application. Images are segregated per user and per entity.
When I use ->upload() the web service returns ALL the images recorded into the metadata table. I would like to filter only the relevant images. I did not find any example or snippet.
Essentially I need to add a where clause somewhere.
This is my code.
Plain text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 Field::inst( 'child_information.document_id' )
->setFormatter( 'Format::ifEmpty' , null )
->upload( Upload::inst( function ( $file, $id ) {

               try {

                   $_directory = getenv('DOCUMENT_PHISICAL_PATH') . '/' . $_SESSION['account_id'] .'/';

                   // Move files into dedicated directory
                   move_uploaded_file( $file['tmp_name'], $_directory . $id . '.' . pathinfo( $file['name'], PATHINFO_EXTENSION) );

               } catch (Exception $e) {
                   echo 'Error: ' . $e->getMessage();
               }

               return $id;
           } )
               ->db( 'document', 'id', array(
                   'original_file_name'    => Upload::DB_FILE_NAME,
                   'file_size'    => Upload::DB_FILE_SIZE,
                   'file_extension' => Upload::DB_EXTN,
                   'type_id' => 'DO',
                   'entity' => 'schedule_document',
                   'account_id' => $_SESSION['account_id']
               ) )
               ->validator( function ( $file ) {
                   return$file['size'] >= 15000000
                       ? "Files must be smaller than 15mB"
                       : null;
               } )
               ->allowedExtensions( [ 'png', 'jpg', 'pdf', 'doc', 'docx' ], "Please upload an image, a PDF/Word file." )
       ),

Could you help?
Thanks,
Melissa

Replies

  • allanallan Posts: 63,201Questions: 1Answers: 10,415 Site admin

    Hi Melissa,

    The Upload class has a where() method that you can use to limit the selection of files to a given condition (account_id in the session in this case presumably).

    Allan

This discussion has been closed.