I'm modifying the story module to create a custom staff content type that basically has a photo, their role and a biography. I've got the module modified and saving the custom data to my custom table except for the image. I've been looking through the image module and the upload module and come across file_check_upload and file_save_upload but I can't seem to get them to work.
/**
* Implementation of hook_prepare
*/
function staff_prepare(&$node) {
$file = file_check_upload('photo');
if ($file) {
$file = file_save_upload('photo', file_directory_path());
$node->photo = $file->filename;
}
}
I thought that this would move the file from the temp folder to the files folder and put the name of the file into the database but I end up with just the full path and filename of the original file on my PC. My file upload field is called 'photo'
Can someone please help me to get this code right, I basically want to have a single photo associated with each staff record and just the filename stored in the record.
Comments
Do I need to enable the upload module?
I've got this working by enabling the upload module, is this module the key or is there something I'm missing from my code? Here's what I've got so far:
One thing it is not doing is storing the filename in the table, can anyone please help?
One thing missing from form hook
Not sure if is the only issue but in the form hook you need
otherwise the file field does not work.
are you sure it isn't
are you sure it isn't supposed to be like this:
$form['attributes'] = array("#enctype" => "multipart/form-data");
This is the correct way..
I used the above snippet and it did not work at all.
Here is the right attribute:
$form['#attributes'] = array("enctype" => "multipart/form-data");
Alot of time was wasted on this.
chris
www.chrisbovard.com | Drupal Developer / Themer
Thank you!
Thank you so very much. I can't believe I missed that. Works perfectly now.
Still having some trouble
Got the file uploading but for some reason the filename still doesn't get stored in the table. When I set $node->photo in the validate hook it seems to have lost its value by the time it gets to the insert or update hook. Anyone spot my glaring error?
It will not be saved in the validate hook
You can not change to form field values in the validate hook. Instead you want to do this in the submit hook (common to both insert and update). And yes, this means running the same code twice since you still want to validate the file.
Thanks
Thanks for that, looks like the documentation needs changing because in the docs for hook_validate it says:
Which is either wrong or I've misunderstood.
Thanks again for the help, now works 100%.
a bit of both.
It is out-of-date.
validate no longer allows changes. Pity.
Mmm. Docs should be updated there.
Search for ways around it. I believe you use 'submit' (which no longer actually submits anything *sigh*)
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
here's what I got
I got this working nicely, though it doesn't update the files table the way I thought it would, but the important thing is it puts the file name in the right field of the table for my custom content type...
I also added the ability to over write existing file with the same name, and delete existing files when the name is different
here are the relevent parts of my code, the rest is pretty much what I got at http://api.drupal.org/api/4.7/file/developer/examples/nodeapi_example.mo...