Create folder for uploaded files

Create folder for uploaded files

lincolncbennettlincolncbennett Posts: 25Questions: 11Answers: 1

Hi.
I am trying to upload many files to dynamically created folders based on the primary id. Basically on selection of table row, if a file is uploaded, create a folder named after the id of the table and upload file to it. I have read https://datatables.net/forums/discussion/31884 but cannot get to work.
Any help would be great.
My PHP........ (upload many)

<?php

include( "lib/DataTables.php" );
use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate;
    
    

Editor::inst( $db, 'yamba' ,'id' )
    ->fields(
     Field::inst( 'yamba.client_status_sort' ),
            
     Field::inst( 'yamba.icon' ),
            
     Field::inst( 'yamba.client_status_change_date' ),
            
     Field::inst( 'yamba.id' ),
            
     Field::inst( 'yamba.year' )
            ->validator( 'Validate::notEmpty' ),
     Field::inst( 'yamba.name' )
            ->validator( 'Validate::notEmpty' ),    
     Field::inst( 'yamba.client_status' ),
            
     
     Field::inst( 'yamba.active_1' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.active_2' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.active_3' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.active_4' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.active_5' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.active_6' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.active_7' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.active_8' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.active_9' )
            ->setFormatter( function ( $val, $data, $opts ) {
                return ! $val ? 0 : 1;
            }),
            Field::inst( 'yamba.contact_relationship_1' ),
            Field::inst( 'yamba.contact_relationship_2' ),
            
            Field::inst( 'yamba.add_contact_1' ),
            
            Field::inst( 'yamba.file_note_1' ))
            
            
            
            
             ->join(
        Mjoin::inst( 'files', 'id' )
            ->link( 'yamba.id', 'users_files.user_id' )
            ->link( 'files.id', 'users_files.file_id' )
            ->fields(
                Field::inst( 'id' )
                
                
                
                                
                            
                    ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/__NAME__' )
                        ->db( 'files', 'id', array(
                            'fileName'    => Upload::DB_FILE_NAME,
                            'fileSize'    => Upload::DB_FILE_SIZE,
                            'webPath'    => Upload::DB_WEB_PATH,
                            'systemPath' => Upload::DB_SYSTEM_PATH
                        ) )
                        ->validator( function ( $file ) {
                            return$file['size'] >= 500000000 ?
                                "Files must be smaller than 50000K" :
                                null;
                        } )
                        ->allowedExtensions( array( 'png', 'jpg', 'pdf', 'jpeg', 'doc', 'docx', 'csv' ), "Please upload an image" )
                        
                    )
            )
    )
    
    
    
    
        
    ->process( $_POST )
    ->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,160Questions: 1Answers: 10,406 Site admin

    Rather than using:

    ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/NAME' )

    You would need to perform a custom upload action that will create the directory (mkdir in PHP) and move the file into the correct place.

    Allan

  • lincolncbennettlincolncbennett Posts: 25Questions: 11Answers: 1

    Hi Al, thanks for the reply. I have been looking at this all day and cannot figure out. I'm sorry. I have changed the PHP as shown below and although there is no error on upload, the folder is not being created or file being uploaded. thanks

    ->join(
            Mjoin::inst( 'files', 'id' )
                ->link( 'yamba.id', 'users_files.user_id' )
                ->link( 'files.id', 'users_files.file_id' )
                ->fields(
                    Field::inst( 'id' )
                    
       ->upload( Upload::inst( function ( $file, $id ) {
           move_uploaded_file( $file['name'], '/funeral/uploads/'.$id );
            return $id;
        } )
            ->db( 'files', 'id', array(
                'fileName' => Upload::DB_FILE_NAME,
                'fileSize' => Upload::DB_FILE_SIZE
            ) )                             
                                
              ->validator( function ( $file ) {
                                return$file['size'] >= 500000000 ?
                                    "Files must be smaller than 50000K" :
                                    null;
                            } )
                            
                            ->allowedExtensions( array( 'png', 'jpg', 'pdf', 'jpeg', 'doc', 'docx', 'csv' ), "Please upload an image" )
                            
                            
                        )
                )
        )
    
    
  • allanallan Posts: 63,160Questions: 1Answers: 10,406 Site admin
    Answer ✓

    I don't see a call to the PHP mkdir() function in your code above. How are you currently attempting to create the directory?

    Allan

  • lincolncbennettlincolncbennett Posts: 25Questions: 11Answers: 1

    I have been trying to implement this example as shown in the linked discussion ;

    $filter = isset($_GET['id'])?$_GET['id']:-1;
      if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/'.$filter)) {
        mkdir($_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/'.$filter, 0777, true);
    }
    
    

    with the custom upload in my last post, I am not seeing any errors when I upload but the file does not upload to the folder specified.

     ->upload( Upload::inst( function ( $file, $id ) {
           move_uploaded_file( $file['name'], '/funeral/uploads/'.$id );
            return $id;
        } )
            ->db( 'files', 'id', array(
                'fileName' => Upload::DB_FILE_NAME,
                'fileSize' => Upload::DB_FILE_SIZE
            ) )     
    

    With the mkdir() above and this code I can save files to the -1 folder which is being created.

      ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/'.$filter.'/__NAME__' )
                            ->db( 'files', 'id', array(
                               'fileName'    => Upload::DB_FILE_NAME,
                                'fileSize'    => Upload::DB_FILE_SIZE,
                                'webPath'    => Upload::DB_WEB_PATH,
                                'systemPath' => Upload::DB_SYSTEM_PATH,
                                
                            ) ) 
    

    I can't seem to figure out how to pass the id (primary) of the table to the php, so the _GET can pass it to the upload () when editor is opened?

    Any help would be great.

    Full code

    <?php
    
    
    // DataTables PHP library and database connection
    include( "lib/DataTables.php" );
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate;
        
    $filter = isset($_GET['id'])?$_GET['id']:'-1';
    if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/'.$filter)) 
    mkdir($_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/'.$filter, 0777, true);
     
         
    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'yamba' ,'id' )
        ->fields(
         Field::inst( 'yamba.client_status_sort' ),
                
         Field::inst( 'yamba.icon' ),
                
         Field::inst( 'yamba.client_status_change_date' ),
                
         Field::inst( 'yamba.id' ),
                
         Field::inst( 'yamba.year' )
                ->validator( 'Validate::notEmpty' ),
         Field::inst( 'yamba.name' )
                ->validator( 'Validate::notEmpty' ),    
         Field::inst( 'yamba.client_status' ),
                
         
         Field::inst( 'yamba.active_1' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.active_2' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.active_3' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.active_4' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.active_5' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.active_6' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.active_7' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.active_8' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.active_9' )
                ->setFormatter( function ( $val, $data, $opts ) {
                    return ! $val ? 0 : 1;
                }),
                Field::inst( 'yamba.contact_relationship_1' ),
                Field::inst( 'yamba.contact_relationship_2' ),
                
                Field::inst( 'yamba.add_contact_1' ),
                
                Field::inst( 'yamba.file_note_1' ))
                
                
                ->join(
            Mjoin::inst( 'files', 'id' )
                ->link( 'yamba.id', 'users_files.user_id' )
                ->link( 'files.id', 'users_files.file_id' )
                ->fields(
                    Field::inst( 'id' )
                    
                                                                
                        ->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/'.$filter.'/__NAME__' )
                            ->db( 'files', 'id', array(
                               'fileName'    => Upload::DB_FILE_NAME,
                                'fileSize'    => Upload::DB_FILE_SIZE,
                                'webPath'    => Upload::DB_WEB_PATH,
                                'systemPath' => Upload::DB_SYSTEM_PATH,
                                
                            ) )
                            
                            ->validator( function ( $file ) {
                                return$file['size'] >= 5000000 ?
                                    "Files must be smaller than 5000K" :
                                    null;
                            } )
                            
                            ->allowedExtensions( array( 'png', 'jpg', 'pdf', 'jpeg', 'doc', 'docx', 'csv' ), "Please upload an image" )
                            
                            
                        )
                )
        )
        
        
        
        
            
        ->process( $_POST )
        
        ->json();
        
    
This discussion has been closed.