PHP Error in custom upload function after updating to latest version

PHP Error in custom upload function after updating to latest version

wouterwouter Posts: 22Questions: 7Answers: 0

Hey all,

After updating to the latest version i get an error when trying to upload a file. using a custom upload function.
I'm not sure where to start debugging. Anyone else running into this issue?

This is the error

    <b>Fatal error</b>:  Uncaught TypeError: str_replace(): Argument #3 ($subject) must be of type array|string, Closure given in php/lib/editor/lib/Editor/Upload.php:728
    Stack trace:
    #0 php/lib/editor/lib/Editor/Upload.php(728): str_replace('__NAME__', 'unnamed.jpg', Object(Closure))
    #1 php/lib/editor/lib/Editor/Upload.php(698): DataTables\Editor\Upload-&gt;_path('unnamed.jpg', '727')
    #2 php/lib/editor/lib/Editor/Upload.php(453): DataTables\Editor\Upload-&gt;_dbExec(Array, Object(DataTables\Database))
    #3 php/lib/editor/lib/Editor.php(1451): DataTables\Editor\Upload-&gt;exec(Object(DataTables\Editor))
    #4 php/lib/editor/lib/Editor.php(1006): DataTables\Editor-&gt;_upload(Array)
    #5 php/lib/editor/lib/Editor.php(709): DataTables\Editor-&gt;_process(Array)
    #6 php/data/dte/contact.php(482): DataTables\Editor-&gt;process(Array)
    #7 php/index.php(80): include('/var/www/vhosts...')
    #8 {main}
      thrown in <b>php/lib/editor/lib/Editor/Upload.php</b> on line <b>728</b><br />

And this is the custom upload function:

            // -----------------------------------------------------------------
            Field::inst( 'contact.vo_photo', 'vo_photo' )
                ->setFormatter( Format::ifEmpty( null ) )
                ->upload(
                    Upload::inst( function ( $file, $id ) use ($db) {

                        // file path
                        $path = DOCROOT.'files/contact/photo/';
                        $file_orig = $id.'.'.pathinfo($file['name'], PATHINFO_EXTENSION);
                        $file_thumb = $id.'.png';

                        // original file
                        move_uploaded_file( $file['tmp_name'], $path.'orig/'.$file_orig );

                        // thumbnail
                        image_thumb( $path.'orig/'.$file_orig, $path.'thumb/'.$file_thumb );

                        // add custom fields
                        $db->update( 'contact_photo',[ "file" => $file_orig, "thumb" => $file_thumb, ], [ "id" => $id ] );

                        //id
                        return $id;
                        })

                    ->db( 'contact_photo', 'id', [
                        'filename'      => Upload::DB_FILE_NAME,
                        'file'          => '',
                        'thumb'         => ''
                        ])


                    ->dbClean( function ( $data ) {
                        $path = DOCROOT.'files/contact/photo/';

                        // Remove the files from the file system
                        for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
                            unlink( $path.'orig/'.$data[$i]['file'] );
                            unlink( $path.'thumb/'.$data[$i]['thumb'] );
                            }

                        // Have Editor remove the rows from the database
                        return true;
                        })

                    ->validator( Validate::fileSize( 5000000, 'Files must be smaller that 5mb' ) )
                    ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
                    ),

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,491Questions: 1Answers: 10,470 Site admin
    Answer ✓

    Hi,

    I believe it was fixed with this commit. I'm going to release 2.1.3 which would include this fix soon (hopefully today), but if you want to try it immediately, you could make that edit in the Editor/Upload.php file that you have.

    Regards,
    Allan

  • wouterwouter Posts: 22Questions: 7Answers: 0

    Hi Allan,

    Thanks! Yes that solves my issues.

    Thank you for the amazing support!!!

  • allanallan Posts: 63,491Questions: 1Answers: 10,470 Site admin

    Awesome - thanks for letting me know.

    I need to issue patch releases more frequently I think!

    Allan

Sign In or Register to comment.