Page 1 of 1

File name fields and dupliacate files

PostPosted: Wed Aug 26, 2009 3:11 am
by andperry
I have a field of widget type "File Name" with the allowed extensions set to a single value (i.e. pdf). I have observed that if I create a new record and upload a file with a name that already exists on the server, '-1' is appended to the whole filename. So for example 'myfile.pdf' gets uploaded as 'myfile.pdf-1'. I appreciate that this is to stop files being inadvertently overwritten, but wouldn't it be more sensible to upload it as 'myfile-1.pdf' thus keeping the proper file extension?

PostPosted: Wed Aug 26, 2009 8:16 am
by shannah
This is strange. It should change it from to myfile-1.pdf not myfile.pdf-1 .

Re: File name fields and dupliacate files

PostPosted: Fri Oct 15, 2010 5:31 pm
by semicon_guy
On a related note to this question, has anyone developed an elegant way to retain -original- file names when uploading the files to the filesystem and not the database? I'd like to keep files on filesystem rather than database, but currently the filename is changed if a duplicate file already exists. This isn't great, because we'd like to be able to have database remember what the original uploaded file name was!

For instance, if I have a file fridge-1.pdf and upload it, later I won't know for sure if its name was really fridge-1.pdf or it was changed from fridge.pdf to fridge-1.pdf.

As a hack, I am thinking of having an additional field called "FileName" and letting user type in file name.

Thanks!

Re: File name fields and dupliacate files

PostPosted: Fri Oct 15, 2010 5:47 pm
by shannah
Use your strategy of adding a filename field but make it hidden and auto fill it with a before save trigger using the name of the uploaded file.

Re: File name fields and dupliacate files

PostPosted: Fri Oct 15, 2010 6:03 pm
by semicon_guy
Good suggestion! With that suggestion, I wonder what will happen if the user re-edits the row in the table.

We probably would want the trigger to be only activated if a new file is uploaded or the file is changed, but not to be triggered if a file remains the same. Otherwise the "good" filename could be overwritten by the "bad" internal filename after a 2nd save.

Re: File name fields and dupliacate files

PostPosted: Fri Oct 15, 2010 6:38 pm
by shannah
Code: Select all
function beforeSave($record){
    if ( @$_FILES['upload_field_name'] ){
        // The file has been uploaded  (assumes your actual container field is named 'upload_field_name'
        $filename = $_FILES['upload_field_name']['name'];
        $record->setValue('filename', $filename);
            // Set your filename field with the name of the file as it was uploaded.
    }
}

Re: File name fields and dupliacate files

PostPosted: Tue Oct 19, 2010 11:07 am
by semicon_guy
Thanks! This got me started, but it needed to be modified as it would clear the value in the hidden form if I re-saved the table row without uploading a new file.

Here is code that worked for me:

Code: Select all
  function beforeSave($record){

    if ( @$_FILES['Attachment'] && @$_FILES['Attachment']['name'] != "" ){
      // The file has been uploaded  (assumes your actual container field is named 'upload_field_name'
      $filename = $_FILES['Attachment']['name'];
      $record->setValue('Attachment__filename', $filename);
      // Set your filename field with the name of the file as it was uploaded.
    }

  }