Hi I am trying to use drupal forms to upload an image as part of the form submission
I added a upload field like so

$form['fundraiser']['image'] = array(
'#type' => 'file',
'#title' => t('Logo'),
'#size' => 30,
'#description' => t('upload your logo'),
);

then added this to the submit function

$dest = 'sites/default/files/fundraisers/images';
$source = 'files[fundraiser]';
$out = file_save_upload($source,$dest, $replace = FILE_EXISTS_RENAME);

but it fails with 0 as the return value.

I have installed the image module

Does someone know what I should do please?

Comments

jabberwok’s picture

I have since figured this out

for anyone looking for this answer,

Most importantly I was forgetting to add the enctype=multipart/form-data

like so

$form['#attributes'] = array('enctype'->'multipart/form-data');

hope this helps someone

digitalramble’s picture

Can something like this be used to upload a video? I'm looking for ways to do this on a node, rather than by "editing" the node itself...

rcombes’s picture

Can you be a bit more specifi about where the ...

$form['#attributes'] = array('enctype'->'multipart/form-data');

... line goes?

I've tried putting it at the beginning of the function that declares the elements of the array and in the function that declares the form's properties (eg. 'page callback', 'access arguments', etc) and both give me the following error

Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ')'

Cheers
Roland Combes

rcombes’s picture

Found the answer to my problem. I'm using Drupal 6 so the correct syntax for the enctype statement was the following...

$form['#attributes']['enctype'] = "multipart/form-data";

... which I placed in the function that declares the form elements.

Cheers
Roland Combes