Seite 1 von 1

UserInput Binary file naming

Verfasst: Mo 21. Mai 2012, 13:44
von Michael_S
Hi,

We are using the <we:userInput type="binary"> to upload PDFs to the backend and would like to use the original file name instead of the one generated by webEdition. For example if we upload a file called 'my-file.pdf' it would be renamed to:

[field name]_[we_ID]_my-file.pdf

I can understand the reasons behind renaming the file but its important for us to keep the original name for particular file types such as firmware updates (.upd or .tgz).

Is there a way to keep the original file name when using <we:userInput type="binary">?

Kind regards,

Michael.

Re: UserInput Binary file naming

Verfasst: Mo 21. Mai 2012, 14:20
von Creutzburg
The only way is to rename the file via php after uploading.

I guess you have a form type=document or type=object, right? Then you'll proceed the form data using we:write on the next page.

You could use the following code within an <we:ifWritten type="XXX">:

Code: Alles auswählen

<we:ifWritten type="object">
  <?php
  // Include classes to edit objects and binary documents
  include_once $_SERVER['DOCUMENT_ROOT'].'/webEdition/we/include/we.inc.php';
  include_once $_SERVER['DOCUMENT_ROOT'].'/webEdition/we/include/we_modules/object/we_objectFile.inc.php';
  include_once $_SERVER['DOCUMENT_ROOT'].'/webEdition/we/include/we_classes/we_binaryDocument.inc.php';

  $currentObject = $GLOBALS['we_object']['formname']->ID; // "formname" is the name of your form
  
  $obj = new we_objectFile();
  
  // initialize written object
  $obj->initByID($currentObject);
  
  // get ID of the binary document
  $connectedDocID = $obj->getElement('file'); // "file" is the name of your object field containing the binary file
  
  if($connectedDocID!=0){
    
    $file = new we_binaryDocument();
    
    // initialize binary file
    $file->initByID($connectedDocID);
    
    $file->setParentID(XXX); // If you want, you could move the file into another directory - if not, just delete this line
    
    $oldFilename = $file->Text;
    // now you could modify the current file name to strip the field name and the id

    $newFilename = "WHATEVER"; // only an example - please use your own converted file name instead

    $file->Filename = $newFilename.$file->Extension;
    $file->Text = $newFilename;
    $file->Path = $file->getParentPath() . (($file->getParentPath() != "/") ? "/" : "") . $file->Text;
    
    // save binary file using new name
    $file->we_save();
  }
  ?>
</we:ifWritten>

Re: UserInput Binary file naming

Verfasst: Mo 21. Mai 2012, 19:36
von Michael_S
Hi Creutzburg,

Thanks for the code example I will give it a try later today and will report back.

Michael.

Re: UserInput Binary file naming

Verfasst: Di 22. Mai 2012, 19:08
von Michael_S
Hi Alex,

The code works well apart from dealing with the file extension. If I upload a file called 'Test-File.pdf' the webEdition renames it to 'DOC_###_Test-File.pdf', your code allows me to remove 'DOC_###_' but duplicates the file extension in the file name in the treemenu ending up with 'Test-File.pdf.pdf'.

I've tried removing the file extension in the renaming process which gives me 'Test-File.pdf' in the treemenu but this breaks the download link. I'm assuming that the file name and extension are separate elements in the treemenu thats why I'm having problems.

Do you have a suggestion on how I would update your code to allow me to change the file name part of the uploaded file?

Kind regards,

Michael.

Re: UserInput Binary file naming

Verfasst: Di 22. Mai 2012, 21:45
von Creutzburg
Well, there was a bug in my code :-(

For your information:
$file->Filename contains the name only, without extension
$file->Extension contains the extension including the dot
$file->Text contains the full name including the extension

This code should work:

Code: Alles auswählen

<we:ifWritten type="object">
  <?php
  // Include classes to edit objects and binary documents
  include_once $_SERVER['DOCUMENT_ROOT'].'/webEdition/we/include/we.inc.php';
  include_once $_SERVER['DOCUMENT_ROOT'].'/webEdition/we/include/we_modules/object/we_objectFile.inc.php';
  include_once $_SERVER['DOCUMENT_ROOT'].'/webEdition/we/include/we_classes/we_binaryDocument.inc.php';

  $currentObject = $GLOBALS['we_object']['formname']->ID; // "formname" is the name of your form
  
  $obj = new we_objectFile();
  
  // initialize written object
  $obj->initByID($currentObject);
  
  // get ID of the binary document
  $connectedDocID = $obj->getElement('file'); // "file" is the name of your object field containing the binary file
  
  if($connectedDocID!=0){
    
    $file = new we_binaryDocument();
    
    // initialize binary file
    $file->initByID($connectedDocID);
    
    $file->setParentID(XXX); // If you want, you could move the file into another directory - if not, just delete this line
    
    $oldFilename = $file->Filename; // was "$file->Text"
    // now you could modify the current file name to strip the field name and the id

    $newFilename = "WHATEVER"; // only an example - please use your own converted file name instead

    $file->Filename = $newFilename;
    $file->Text = $newFilename.$file->Extension;
    $file->Path = $file->getParentPath() . (($file->getParentPath() != "/") ? "/" : "") . $file->Text;
    
    // save binary file using new name
    $file->we_save();
  }
  ?>
</we:ifWritten>
Hope that helps!

Regards,
Alex

Re: UserInput Binary file naming

Verfasst: Mi 23. Mai 2012, 00:20
von Michael_S
Hi Alex,

Thanks the file renaming is correct now, a very useful script.

However it looks like the renaming has upset something because if I go to edit the object using a listview <we:a edit="object"> the binary field is missing the file name (screen shot attached) The binary file name is displayed in the listview and I can see it in the object.

Regards,

Michael.

Re: UserInput Binary file naming

Verfasst: Mi 23. Mai 2012, 09:15
von Creutzburg
if I go to edit the object using a listview <we:a edit="object"> the binary field is missing the file name (screen shot attached
Did this work before renaming the file?

When you open the object in WebEdition, the file is displayed correctly within the object?

Regards,
Alex

Re: UserInput Binary file naming

Verfasst: Do 24. Mai 2012, 12:09
von Michael_S
Hi Alex,

Yes, before the re-naming script was applied the file name was displayed in the userInput binary box but the original file name (original-name.pdf) not the webEdition file name ([field name]_[we_id]_original-name.pdf) was displayed.

The object shows the correct file name after re-naming (original-name.pdf).

Thanks,

Michael.