Rename uploaded file before moving to final location?

Rename uploaded file before moving to final location?

jhenryjhenry Posts: 11Questions: 1Answers: 0
edited November 2018 in Free community support

Is it possible to rename a file based on an editor input field before uploading it? I was thinking of using the custom upload action like below.

I want to upload a file (with any name) and rename it using another field from the editor before it is stored in its final location.

Field::inst( 'transtest.SerialNo' )
        ->validator( Validate::notEmpty( ValidateOptions::inst()
            ->message( 'A serial number is required' ) ) ),
Field::inst( 'images.name' )
        ->upload(
                Upload::inst( function ( $file, $id ) {

                    $SystemPath = '\\\\SHARES\\IMAGES\\GIS\\Pictures\\';
                    $WebPath = 'http://appsserv/app/power/pictures/';
                    $extension = '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
                        
                    -->
                    $NewName = (the transtest.SerialNo from above);
                    -->

                    move_uploaded_file( $file['tmp_name'], $SystemPath.$NewName.$extension);
                        return $id
                } )
        ->db( 'images', 'name', array(
                'images.name'  => Upload::DB_FILE_NAME,
                'images.size'  => Upload::DB_FILE_SIZE,
        ) )
        ->validator( Validate::fileSize( 5000000, 'Files must be smaller that 5 MB' ) )
        ->validator( Validate::fileExtensions( array( 'png', 'jpg', 'jpeg', 'gif' ), "Please upload an image" ) )
)
->leftJoin( 'images', 'transtest.image', '=', 'images.name' )

Is there a way to access the transtest.SerialNo field in order to rename the uploaded file using it?

I attempted this route:

https://datatables.net/forums/discussion/44969/editor-upload-file-exists-confirm-overwrite-prompt

the solution all the way at the bottom...but I couldnt get the writeCreate or writeEdit to fire. I verified the custom upload was writing to the db but they would not fire.

Replies

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    Is it possible to rename a file based on an editor input field before uploading it?

    Not really. You could use the ajax.data option for the upload action to get the field value and submit that to the server, but you need to consider:

    1. The user might then change that field input after the upload
    2. The user might not enter anything into that field until after the upload (i.e. it is blank).

    My suggestion would be to store the file with a unique file name (e.g. the primary key) and then use the meta data in the database when serving it back to set the download file name.

    Allan

  • jhenryjhenry Posts: 11Questions: 1Answers: 0

    Allan,

    Thanks for the reply. I apologize; I had not seen the post a few days before this one with basically the same question by DareenRW.....when I did my search on the topic it did not come up.

    I am seeing now that the file upload is basically a separate process from an actual data update and thus why it would be possible for the user to change the data after the upload.

    I appreciate the suggestion on how to store it but that wont work for my situation. The files being uploaded are dataplate pictures of serialized equipment that we have out in the field...about 17,000 of them at the moment. Multiple pieces of software link to these images with the ability to add/replace them when needed. As such, it was decided early on that we would use the serial numbers as the file names to make it easy for all the different suites to access them uniformly.

    As it stands, we have all the images in a specific folder on the server. When we open a record for a particular equipment location, the serial number is part for the data displayed. From there we just look through the image folder and if an image exists with that serial number as its file name, we display a link or thumbnail depending on which program is accessing it. To add or remove it, we just do a temporary upload of the new image then when the record is updated the image is renamed and stored in the image folder on the server. If the record is simply closed without no update, the new image would simply be dropped.

    I already have a working page to do this for me, I just wanted to switch to Datatables because its much more aesthetically pleasing.

    Thanks for the advice.

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    Just thought of a possible option if you aren't bothered about having a preview of the image in the input form.

    When you upload the image, store it in a temporary location (you'd probably need to record where in the database of image meta information). Then when the main form is submitted move the file from that temporary location to its final location, since you have the submitted form data at that point. The postCreate / postEdit server-side events could be used for that.

    Then when the record is displayed in the table it will be in the correct location. That method could also cope with a change in the serial number without reuploading the image.

    Allan

  • jhenryjhenry Posts: 11Questions: 1Answers: 0

    Ahhh, good call. That should work out the way I need it to. Ill give it a go...Thanks Allan

This discussion has been closed.