File name fields and dupliacate files

A place for users and developers of the Xataface to discuss and receive support.

File name fields and dupliacate files

Postby andperry » Wed Aug 26, 2009 3:11 am

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?
andperry
 
Posts: 57
Joined: Wed Feb 18, 2009 9:20 am
Location: Wirral, UK

Postby shannah » Wed Aug 26, 2009 8:16 am

This is strange. It should change it from to myfile-1.pdf not myfile.pdf-1 .
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: File name fields and dupliacate files

Postby semicon_guy » Fri Oct 15, 2010 5:31 pm

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!
semicon_guy
 
Posts: 16
Joined: Wed Oct 13, 2010 5:05 pm

Re: File name fields and dupliacate files

Postby shannah » Fri Oct 15, 2010 5:47 pm

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.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: File name fields and dupliacate files

Postby semicon_guy » Fri Oct 15, 2010 6:03 pm

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.
semicon_guy
 
Posts: 16
Joined: Wed Oct 13, 2010 5:05 pm

Re: File name fields and dupliacate files

Postby shannah » Fri Oct 15, 2010 6:38 pm

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.
    }
}
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: File name fields and dupliacate files

Postby semicon_guy » Tue Oct 19, 2010 11:07 am

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.
    }

  }
semicon_guy
 
Posts: 16
Joined: Wed Oct 13, 2010 5:05 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 35 guests

Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved