File uploads to dynamically created folders.

File uploads to dynamically created folders.

tomekkietomekkie Posts: 30Questions: 6Answers: 1
edited December 2015 in Editor

I would like to place the uploaded files into dynamically created folders.

->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__' )

The Upload class doesn't seem to work if the upload folder does not exist.
Should I make a custom action for that or rather modify the Upload class making that to create upload folder if not exists?

Additional guestion: how - the simplest - to add an action to copy the uploaded file into 'upload/original/' subfolder?
I would like to downsize the uploaded files and use the downsized version for displaying in the table, while keeping the originals in 'original' subfolder.
Someone - on this forum - was making the thumbnails on 'postCreate' event, is there a way to make them on upload?

This question has accepted answers - jump to:

Answers

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    Answer ✓

    Don't know if it will help you, but I did something similar recently.

    I have a set of image galleries where I want to add images to a particular folder, according to the gallery selected.

    I pass the gallery id to my ajax page with the editor instance then create a folder, named after the gallery id if it doesn't already exist

    $filter = isset($_GET['GalleryID'])?$_GET['GalleryID']:-1;//filter recordset according to currently selected gallery
    
     //create folder for uploaded image if it doesn't already exist
    
     if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/images/gallery/'.$filter)) {
        mkdir($_SERVER['DOCUMENT_ROOT'].'/images/gallery/'.$filter, 0777, true);
        }
    
    // Build our Editor instance and process the data ...
      $data = Editor::inst( $db, 'tblgalleryimages', 'GalleryImageID' )
    ->field(
        ...
    
  • tomekkietomekkie Posts: 30Questions: 6Answers: 1
    edited December 2015

    Thanks, @crush123.
    The insertion of 2 lines into _actionExec function inside Upload script:

    $uploadDir = dirname($to);
    if (!file_exists($uploadDir)) {mkdir($uploadDir, 0777, true);
    

    sorted the non-existing folder problems for me.

    But I would prefer to go along the custom action way:

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

    However I can not work out how to put the web_path and system_path into the database for the purpose of displaying that in the table or latter deletion etc, because Upload::DB_WEB_PATH and Upload::DB_SYSTEM_PATH do not work in Upload->action() case.

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    i don't store the folder path in the database at all, only the file name.

    In the above instance, I create the path on my php page using the images folder and the gallery id, eg /images/gallery/id/filename

  • tomekkietomekkie Posts: 30Questions: 6Answers: 1

    Thanks, @crush 123, I think I can also go that way.
    I am going to subject the uploaded files to some complex processing, like .eps and .pdf to bitmap conversions, size adaptations, etc, so then I will have to add custom postRemove actions to cleanup after that.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    However I can not work out how to put the web_path and system_path into the database for the purpose of displaying that in the table or latter deletion etc

    You are correct - if you want to use any path information in the database you'd need to write it in using an UPDATE statement. I realise this is a bit of a pain - I've been thinking of ways to allow the path information to be passed back to the action caller and will be looking into that for the next release. Not sure that it will be 1.5.4, but I'll post back if so.

    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Just been looking into this and its not something I'm going to include in Editor's Upload class at the moment, however, it is relatively easy to do with your own command:

    ->upload( Upload::inst( function ( $file, $id ) use ( $db ) {
            move_uploaded_file( $file['tmp_name'], '/uploads/'.$id );
    
            $db->update( 'image', [ 'path' => '/uploads/'.$id ], [ 'id' => $id ] );
    
            return $id;
        } )
        ->db( 'image', 'id', array(
            'fileName' => Upload::DB_FILE_NAME,
            'fileSize' => Upload::DB_FILE_SIZE
        ) )
    )
    

    Allan

  • tomekkietomekkie Posts: 30Questions: 6Answers: 1
    edited December 2015

    Great. Thanks for the code example.

This discussion has been closed.