Development

HowToRenameFileAfterUpload (diff)

You must first sign up to be able to contribute.

Changes from Version 1 of HowToRenameFileAfterUpload

Show
Ignore:
Author:
cocomiel (IP: 190.139.226.11)
Timestamp:
02/12/10 00:02:59 (8 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HowToRenameFileAfterUpload

    v0 v1  
     1= How to rename a file after uploading it ? = 
     2 
     3In this example you will see how to upload a file and rename if afterwards. In the new file name you will optionally be able to include properties of the object you are saving. 
     4 
     5Imagine you have a `Swimsuit` class. This class can hold an `image` but after you upload it you want the filename to contain the `Swimsuit` id. 
     6 
     7Start by setting up your form and overriding the `save()` method 
     8{{{ 
     9#!php 
     10<?php  
     11// lib/form/doctrine/SwimsuitForm.class.php 
     12 
     13class SwimsuitForm extends BaseSwimsuitForm 
     14{ 
     15 
     16  public function configure () 
     17  { 
     18    $this->widgetSchema['image'] = new sfWidgetFormInputFileEditable( 
     19    array( 
     20    'label' => 'Foto', 
     21    'file_src' => '/uploads/' . $this->getObject()->getImage(), 
     22    'is_image' => true, 
     23    'edit_mode' => ! $this->isNew(), 
     24    'template' => '<div>%file%<br />%input%<br />%delete_label% %delete%</div>' 
     25    )); 
     26 
     27    $this->validatorSchema['image'] = new sfValidatorFile(array( 
     28    'required' => false, 
     29    'path' => sfConfig::get('sf_upload_dir'), 
     30    'mime_types' => 'web_images', 
     31    'validated_file_class' => 'SwimsuitValidatedFile' 
     32    )); 
     33 
     34    $this->validatorSchema['image_delete'] = new sfValidatorPass(); 
     35  } 
     36 
     37  public function save ($con = null) 
     38  { 
     39    // Get the uploaded image 
     40    $image = $this->getValue('image'); 
     41    $swimsuit = $this->getObject(); 
     42 
     43    if ($image) 
     44    { 
     45      $image->save($swimsuit->Catalogue->getYear() . DIRECTORY_SEPARATOR . $swimsuit->getArticle() . '.jpg'); 
     46    } 
     47 
     48    return parent::save($con); 
     49  } 
     50 
     51} 
     52}}} 
     53 
     54First thing to note is that you are passing a `validated_file_class` option to your `sfValidatorFile`. This enables you to use your own custom class to validate the file. 
     55 
     56The second thing to note is the use of the `getObject()` method to get a reference to the object about to be inserted in the database. 
     57 
     58Next, you need to extend the `sfValidatedFile` class to match your requirements. 
     59 
     60{{{ 
     61#!php 
     62<?php  
     63// lib/validator/SwimsuitValidatedFile.php 
     64 
     65class SwimsuitValidatedFile extends sfValidatedFile { 
     66 
     67  private $savedFilename = null; 
     68 
     69  // Override sfValidatedFile's save method 
     70  public function save($file = null, $fileMode = 0666, $create = true, $dirMode = 0777) { 
     71    // This makes sure we use only one savedFilename (it will be the first) 
     72    if ($this->savedFilename === null) $this->savedFilename = $file; 
     73 
     74    // Let the original save method do its magic :) 
     75    return parent::save($this->savedFilename, $fileMode, $create, $dirMode); 
     76  } 
     77} 
     78 
     79}}} 
     80 
     81The method `SwimsuitValidatedFile::save()` actually gets executed twice; once in your overriden `SwimsuitForm::save()` method and once in the original `BaseSwimsuitForm::save()` method. That is why you need to store the filename that gets passed to it the first time and the use that.