2008年8月25日星期一

Re: [fw-mvc] Zend_Form_Element_File upload issues

Hi,

You can somehow automate the process by using this (not sure if it's the
most appropriated one)

if ($form->isValid($data)) {
{
//Model stuff
}else{

foreach ($form->getElements() as $value)
{
if($value->helper=='formFile')
{
unlink($value->getValue());
}
}
}

This can be more elaborated and maybe extend Zend_Form and create a function

class My_Form extends Zend_Form
{

function deleteTempFiles()
{
foreach ($this->getElements() as $value)
{
if($value->helper=='formFile')
{
unlink($value->getValue());
}
}
}

}


And you can simplify the code on validation by using just

if ($form->isValid($data)) {
{
//Model stuff
}else{
$form->deleteTempFiles();
}

Regards,


Matthew Weier O'Phinney-3 wrote:
>
> -- enportugal <boas2@sapo.pt> wrote
> (on Monday, 25 August 2008, 11:26 AM -0700):
>> When I upload a file I send it to a default location and then within the
>> model to a final one. Of course the file has to be uploaded, my bad....
>>
>> But this is what I do when the form fails to validate
>>
>> if ($form->isValid($data)) {
>> {
>> //Model stuff
>> }else{
>> unlink($form->file->getValue());
>> }
>
> Yep -- this is what I recommended to Adam Jensen as well.
>
>
>> Matthew Weier O'Phinney-3 wrote:
>> >
>> > -- enportugal <boas2@sapo.pt> wrote
>> > (on Monday, 25 August 2008, 10:36 AM -0700):
>> >> 1.The file is only uploaded when the form validation passes, not the
>> >> element
>> >> itself
>> >
>> > Not true -- if the element validates, the file is uploaded and moved to
>> > its appropriate location. This is part of the element validation.
>> >
>> >> 2. A stated here,
>> >>
>> http://framework.zend.com/manual/en/zend.form.standardElements.html#zend.form.standardElements.file,
>> >> the rename filter will be added in future versions. However, you can
>> >> still
>> >> change the file name manually after the file upload using php function
>> >> rename.
>> >>
>> >> Regards,
>> >>
>> >>
>> >> Adam Jensen-2 wrote:
>> >> >
>> >> > Hello!
>> >> >
>> >> > Just started using Zend_Form_Element_File on one of my file upload
>> >> forms,
>> >> > and I noticed a couple of things that concern me a bit. (I
>> apologize
>> >> if
>> >> > these have already been mentioned; I couldn't find them in the bug
>> >> > tracker,
>> >> > but I figured I'd ask here first.)
>> >> >
>> >> > There does not seem to be any way to keep the file element from
>> moving
>> >> the
>> >> > uploaded file to its target destination in the event of certain
>> kinds
>> >> of
>> >> > errors. There are two error situations I'm most concerned with:
>> >> >
>> >> > (1) Suppose that the file element isValid(), but the other elements
>> in
>> >> the
>> >> > form are not. The file still gets uploaded to the server, correct?
>> To
>> >> > me,
>> >> > this doesn't seem like a good idea. Consider a form which
>> corresponds
>> >> to
>> >> > a
>> >> > record in a database table. If the user submits the form and three
>> >> values
>> >> > are valid while the remaining six are not, typically we don't write
>> >> > anything
>> >> > at all to the database (not even the valid values). Now, what if
>> one
>> >> of
>> >> > those valid elements is a file instead? Personally I think the file
>> >> > should
>> >> > not be uploaded, since none of the other valid values get stored
>> >> either.
>> >> >
>> >> > (2) What happens when someone uploads a file that results in a name
>> >> > conflict
>> >> > in the destination folder? For instance, if my destination folder
>> >> already
>> >> > contains a file called "my_upload.txt" and a user uploads another
>> file
>> >> > called "my_upload.txt", currently the existing file simply gets
>> >> > overwritten. Is there a way to keep this from happening? Seems
>> like
>> >> it
>> >> > could be built into either a validator or a filter; the validator
>> would
>> >> > simply block conflicting files from upload, while the filter would
>> >> somehow
>> >> > change the name of the new file so that it no longer conflicted (of
>> >> > course,
>> >> > according to the manual, this kind of filtering is not yet
>> supported,
>> >> > right?)
>> >> >
>> >> > Just thought I'd mention these things...what does everyone else
>> think?
>
> --
> Matthew Weier O'Phinney
> Software Architect | matthew@zend.com
> Zend Framework | http://framework.zend.com/
>
>

--
View this message in context: http://www.nabble.com/Zend_Form_Element_File-upload-issues-tp19147483p19150236.html
Sent from the Zend MVC mailing list archive at Nabble.com.

没有评论: