This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » symfony 1.3 and 1.4 » Image upload won't work
Image upload won't work [message #92576] Sat, 30 January 2010 14:24 Go to next message
Bart00  is currently offline Bart00
Messages: 35
Registered: January 2010
Member
Hi

I'm trying to create a user avatar upload function, but it won't work. I have my code pasted at

http://pastebin.com/m231721b4

All values are saved correctly, but the image does not appear in it's folder (upload/avatars).

As you can see it has no column in the schema. Does that have anything to do with it? The image will be simply renamed to '$uid.png', so it doesn't need a column.

How do I make this work?
Re: Image upload won't work [message #92577 is a reply to message #92576 ] Sat, 30 January 2010 14:33 Go to previous messageGo to next message
hopscotch1978  is currently offline hopscotch1978
Messages: 258
Registered: January 2010
Location: Spain
Faithful Member
Looking at your actions code, I think you need to save the temporary file the form produces into a permanent location or else it's lost as soon as your actions file php finishes executing.

Check here: http://www.symfony-project.org/forms/1_4/en/02-Form-Validati on

See the last section on the page titled "File upload" and see how it deals with an uploaded file:

$file = $this->form->getValue('file');
$filename = 'uploaded_'.sha1($file->getOriginalName());
$extension = $file->getExtension($file->getOriginalExtension());
$file->save(sfConfig::get('sf_upload_dir').'/'.$filename.$extension);


Works perfectly for me. The SF_UPLOAD_DIR is some type of a symfony constant that you can also customize. Goes to /web/uploads/ by default I think.
Re: Image upload won't work [message #92581 is a reply to message #92577 ] Sat, 30 January 2010 14:48 Go to previous messageGo to next message
Bart00  is currently offline Bart00
Messages: 35
Registered: January 2010
Member
Thanks for your reply. Your explanation sounds logical Smile Yet, I run into a new problem. I changed my action into the following:

public function executeEditProfile(sfWebRequest $request) {
  $this->form = new profileForm($this->getUser()->getGuardUser());
  if ($request->isMethod('post')) {
    $this->form->bind(
    $request->getParameter($this->form->getName()),
    $request->getFiles($this->form->getName())
  );
  if ($this->form->isValid()) {
    $avatar = $this->form['profile']->getValue('avatar');
    $filename = $this->getUser()->getGuardUser()->getId().'.'.$avatar->getExtension();
    $avatar->save(sfConfig::get('sf_upload_dir').'/avatars/'.$filename);
    $this->form->save();
    
    $this->getUser()->setFlash('notice', 'Je profiel is opgeslagen!');
  }
  else {
    $this->getUser()->setFlash('error', 'Je profiel is niet opgeslagen. Kijk hieronder wat er fout zou kunnen zijn gegaan');
  }
}


Yet now I get the error "Fatal error: Call to a member function getExtension() on a non-object in C:\websites\tvr\apps\frontend\modules\user\actions\actions.c lass.php on line 22"

Any ideas?

(edit- i just noticed I forgot to add 'enctype="multipart/form-data"' to the <form> tag, but after adding it, I still get the same error)

[Updated on: Sat, 30 January 2010 14:50]

Re: Image upload won't work [message #92583 is a reply to message #92581 ] Sat, 30 January 2010 14:59 Go to previous messageGo to next message
hopscotch1978  is currently offline hopscotch1978
Messages: 258
Registered: January 2010
Location: Spain
Faithful Member
I think your problem is here:

$avatar = $this->form['profile']->getValue('avatar');


Try instead:

$avatar = $this->form->getValue('avatar');

Re: Image upload won't work [message #92584 is a reply to message #92583 ] Sat, 30 January 2010 15:03 Go to previous messageGo to next message
Bart00  is currently offline Bart00
Messages: 35
Registered: January 2010
Member
'profile' is the embedded form and avatar is in there. Yet, I tried what you just posted, but I get the exact same error Sad
Re: Image upload won't work [message #92585 is a reply to message #92584 ] Sat, 30 January 2010 15:06 Go to previous messageGo to next message
hopscotch1978  is currently offline hopscotch1978
Messages: 258
Registered: January 2010
Location: Spain
Faithful Member
Ok, well it seems the problem is either here:

$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName())



or here:

$avatar = $this->form['profile']->getValue('avatar');


So I'd start by breaking those down into simpler components, var_dumping and isolating your problem. Shouldn't be too hard.
Re: Image upload won't work [message #92587 is a reply to message #92585 ] Sat, 30 January 2010 15:22 Go to previous messageGo to next message
Bart00  is currently offline Bart00
Messages: 35
Registered: January 2010
Member
Apparently file uploads don't work in merged forms, some kind of bug, apparently. I put the file upload in the main form, and it worked fine (was no problem as it is not bound to any class anyway).

http://forum.symfony-project.org/index.php/m/88206/

Thanks for your help!
Re: Image upload won't work [message #92588 is a reply to message #92587 ] Sat, 30 January 2010 15:23 Go to previous messageGo to next message
hopscotch1978  is currently offline hopscotch1978
Messages: 258
Registered: January 2010
Location: Spain
Faithful Member
I see, that's good to know for future.

Glad you got it working.
Re: Image upload won't work [message #92589 is a reply to message #92588 ] Sat, 30 January 2010 15:30 Go to previous messageGo to next message
Bart00  is currently offline Bart00
Messages: 35
Registered: January 2010
Member
small question left:

As 'avatar' does not belong to any table column, I now get the error 'Unknown record property / related component "avatar" on "sfGuardUser"'. How can I get $form->save() to skip this value?
Re: Image upload won't work [message #92591 is a reply to message #92589 ] Sat, 30 January 2010 15:53 Go to previous messageGo to next message
hopscotch1978  is currently offline hopscotch1978
Messages: 258
Registered: January 2010
Location: Spain
Faithful Member
Umm... If I understand the question correctly, it might be expecting 'avatar' as part of your profileForm class.

It's going to get complicated to understand your full situation especially as you've updated your code now but maybe you could do something like this (simplified!):

$profile = // fetch this profile from database
$this->form = new profileForm($profile);

// if IsValid
$values = $this->form->getValues();
$profile->setUsername($values['username']);
$profile->setWhatever($values['whatever']);
$profile->save();

// do avatar stuff here, doesn't relate to saving


Hope that makes sense.


Re: Image upload won't work [message #103235 is a reply to message #92576 ] Thu, 29 July 2010 12:02 Go to previous message
meta_tommy  is currently offline meta_tommy
Messages: 13
Registered: February 2010
Location: Bulgaria
Junior Member
Greetings ,
how you deal with uploading avatar in the backend ? Overwrite the process method from the auto generated action ?
Previous Topic:Alternative path for Doctrine templates
Next Topic:access user session from routing class
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software