UserInput Binary file naming

Michael_S
Senior Member
Beiträge: 160
Registriert: Mi 4. Feb 2004, 02:39
Wohnort: London / UK
Kontaktdaten:

UserInput Binary file naming

Beitragvon Michael_S » Mo 21. Mai 2012, 13:44

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.
Production Area
London, UK
http://productionarea.com

Creutzburg
Senior Member
Beiträge: 425
Registriert: Do 1. Jan 1970, 02:00
Wohnort: Dresden
Kontaktdaten:

Re: UserInput Binary file naming

Beitragvon Creutzburg » Mo 21. Mai 2012, 14:20

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>
http://www.xport.de – Internet-Agentur für Hotels
http://www.domainpreisvergleich.de – Domain-Preisvergleich inkl. Verfügbarkeits-Check – ist meine Wunschdomain noch frei, und wo registriere ich sie am günstigsten?

Michael_S
Senior Member
Beiträge: 160
Registriert: Mi 4. Feb 2004, 02:39
Wohnort: London / UK
Kontaktdaten:

Re: UserInput Binary file naming

Beitragvon Michael_S » Mo 21. Mai 2012, 19:36

Hi Creutzburg,

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

Michael.
Production Area
London, UK
http://productionarea.com

Michael_S
Senior Member
Beiträge: 160
Registriert: Mi 4. Feb 2004, 02:39
Wohnort: London / UK
Kontaktdaten:

Re: UserInput Binary file naming

Beitragvon Michael_S » Di 22. Mai 2012, 19:08

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.
Production Area
London, UK
http://productionarea.com

Creutzburg
Senior Member
Beiträge: 425
Registriert: Do 1. Jan 1970, 02:00
Wohnort: Dresden
Kontaktdaten:

Re: UserInput Binary file naming

Beitragvon Creutzburg » Di 22. Mai 2012, 21:45

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
http://www.xport.de – Internet-Agentur für Hotels
http://www.domainpreisvergleich.de – Domain-Preisvergleich inkl. Verfügbarkeits-Check – ist meine Wunschdomain noch frei, und wo registriere ich sie am günstigsten?

Michael_S
Senior Member
Beiträge: 160
Registriert: Mi 4. Feb 2004, 02:39
Wohnort: London / UK
Kontaktdaten:

Re: UserInput Binary file naming

Beitragvon Michael_S » Mi 23. Mai 2012, 00:20

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.
Dateianhänge
Missing file name in userInput type binary after file renaming using script in this thread.
Missing file name in userInput type binary after file renaming using script in this thread.
Screen-shot-2012-05-22-at-23.10.28.jpg (73.02 KiB) 6588 mal betrachtet
Production Area
London, UK
http://productionarea.com

Creutzburg
Senior Member
Beiträge: 425
Registriert: Do 1. Jan 1970, 02:00
Wohnort: Dresden
Kontaktdaten:

Re: UserInput Binary file naming

Beitragvon Creutzburg » Mi 23. Mai 2012, 09:15

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
http://www.xport.de – Internet-Agentur für Hotels
http://www.domainpreisvergleich.de – Domain-Preisvergleich inkl. Verfügbarkeits-Check – ist meine Wunschdomain noch frei, und wo registriere ich sie am günstigsten?

Michael_S
Senior Member
Beiträge: 160
Registriert: Mi 4. Feb 2004, 02:39
Wohnort: London / UK
Kontaktdaten:

Re: UserInput Binary file naming

Beitragvon Michael_S » Do 24. Mai 2012, 12:09

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.
Production Area
London, UK
http://productionarea.com


Zurück zu „DB / Object Module“

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 2 Gäste