+ Reply to Thread
Results 1 to 6 of 6

Thread: Zend Form Element File > Rename... working but with a question...

  1. #1
    Nitecon is offline Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Default Zend Form Element File > Rename... working but with a question...

    Hey folks,

    My form element works just fine, with one exception... I wrote a small function to rename a particular photo to what I want although, as soon as the form is generated it automatically assigns the value, this of course is not too big a deal but it's just causing me some frustration, and I think I've been staring at the problem for a little too long and just need some fresh eyes.

    So here is the form element itself :
    [PHP] $photo = new Zend_Form_Element_File('photo');
    $photo->setLabel('Upload your photo.')
    ->setRequired(false)
    ->setDestination('/path/to/full/permission/images/')
    ->addValidator('Size', false, 102400)
    ->addValidator('Extension', false, 'jpg,png,gif')
    ->addFilter('Rename',
    array('source' => $this->file,
    'target' => "/path/to/full/permission/images/'".$this->formRename($this->file),
    'overwrite' => true));[/PHP]

    And now the function that renames the actual file to something more or less of what I want :
    [PHP] public function formRename($filename)
    {
    $path = $_SERVER['REQUEST_URI'];
    $id = basename($path);
    $ext = substr($filename, strrpos($filename, '.') + 1);
    $newName = $id.'_photo.'.$ext;
    return $newName;
    }[/PHP]
    As you can see I just basically get the ID by the url location and assign that as the users id, then add the _photo suffix, and add the extension to what the original file was....

    Problem remains that as soon as the form loads the $photo section is now "x_photo." No matter if the user adds a file the file name stays the same... Granted the images do show up due to fairly smart browsers, but still this is something I would like to see fixed properly and I'm just staring at code at this point that looks right...
    Last edited by Nitecon; 11-21-2008 at 10:01 PM. Reason: Fixed a quotation mark that distorted the php

  2. #2
    thomas is offline Senior Member
    Join Date
    Aug 2008
    Posts
    173

    Default

    Just a stupid question myself:

    Do you really think that the file element is named "$this->file" ?
    By simply omitting source, the file element will use it's own name.
    Greetings
    Thomas Weidner
    I18N Team Leader, Zend Framework
    http://www.thomasweidner.com

  3. #3
    Nitecon is offline Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Default

    I do realize it will use it's own name, but there are 2 problems here.

    1. The form automatically calls the fromRename() function on initialization not on submit...

    2. I need to change the file name, and at the same time get the extension, since I am allowing 3 different extensions for upload.

    So... if userx for instance uploads Myname.jpg, I need to parse the entire file with extension, rename the file to 1_photo.jpg, while keeping the extension as is. That way when userY uploads APhoto.gif, I can still get the extension. The first part is easy. It's getting the actual extension. So the way I see it there can be 2 solutions to this problem... The first being to find a way to hook the actual file name "onchange" (as in when the user hits the browse button and the value changes to something that is NotEmpty) or by adding some sort of filter or validator or action to getExtension on the particular form field.

    Now here is the swing part of it, if the user does not decide to upload a picture, because of the fact that the formRename() runs on init, there will automatically be a value, so the form element will automatically upload an empty file, so there has to be a check instead of using NotEmpty, but rather something like NotEmpty && OnChange.

    If this doesn't make any sense try it out yourself, I have tried just about everything so far, but I just can't find a way to get the extension

  4. #4
    Nitecon is offline Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Default

    For now what I did to get around this issue is to instead of rename the file, make a directory by $id and store files in there. Although it does exactly what I need, I would still like to find out still how to manipulate the rename function or at least how to stop filters/validations from happening until the form actually submits.

  5. #5
    thomas is offline Senior Member
    Join Date
    Aug 2008
    Posts
    173

    Default

    This is not really a issue of ZF but a handling problem of yourself.

    When you want to add the filter after the file has been received you must do it in this order.

    When adding the filter before the file has been received, like you did it at initiation of the form, it is impossible for the filter to know the real uploaded name as you fixed source with the filename.

    You will have to:
    * Check if the file was uploaded
    * Check if the file is valid
    * Add the filter
    * Receive the file

    Simply do not receive the file element when no file was given
    Greetings
    Thomas Weidner
    I18N Team Leader, Zend Framework
    http://www.thomasweidner.com

  6. #6
    Nitecon is offline Junior Member
    Join Date
    Nov 2008
    Posts
    19

    Default

    Thats exactly the answer I wanted

    I got so stuck on the form itself that I didn't even think about doing that, and just kept changing items left and right until eventually I confused myself to no end!

    Thanks Thomas!

+ Reply to Thread

Similar Threads

  1. Form File Rename
    By banderon in forum General Q&A on Zend Framework
    Replies: 10
    Last Post: 08-11-2010, 07:13 AM
  2. Add filter to zend form file element
    By blange in forum Core Infrastructure
    Replies: 2
    Last Post: 12-11-2009, 01:41 AM
  3. form validation with file element
    By alexjet in forum Core Infrastructure
    Replies: 6
    Last Post: 10-25-2008, 12:33 AM
  4. Rename file upload by Zend_Form_Element_File
    By kusanagi in forum General Q&A on Zend Framework
    Replies: 3
    Last Post: 09-01-2008, 06:18 AM
  5. Multiple Form Element Decorators In Config File
    By qhas in forum Model-View-Controller (MVC)
    Replies: 1
    Last Post: 04-08-2008, 02:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts