Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-21-2008, 09:47 PM
Junior Member
 
Join Date: Nov 2008
Posts: 14
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 Code:
        $photo = new Zend_Form_Element_File('photo');
        
$photo->setLabel('Upload your photo.')
                ->
setRequired(false)
                ->
setDestination('/path/to/full/permission/images/')
                ->
addValidator('Size'false102400)
                ->
addValidator('Extension'false'jpg,png,gif')
                ->
addFilter('Rename',
                   array(
'source' => $this->file,
                         
'target' => "/path/to/full/permission/images/'".$this->formRename($this->file),
                         
'overwrite' => true)); 
And now the function that renames the actual file to something more or less of what I want :
PHP Code:
    public function formRename($filename)
    {
            
$path $_SERVER['REQUEST_URI'];
            
$id basename($path);
            
$ext substr($filenamestrrpos($filename'.') + 1);
            
$newName $id.'_photo.'.$ext;
            return 
$newName;
        } 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-22-2008, 07:58 AM
Senior Member
 
Join Date: Aug 2008
Posts: 128
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-22-2008, 01:45 PM
Junior Member
 
Join Date: Nov 2008
Posts: 14
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-22-2008, 03:44 PM
Junior Member
 
Join Date: Nov 2008
Posts: 14
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-22-2008, 05:00 PM
Senior Member
 
Join Date: Aug 2008
Posts: 128
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 11-22-2008, 05:20 PM
Junior Member
 
Join Date: Nov 2008
Posts: 14
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 02:18 AM.