By Perad on
$form['image'] = array(
'#type' => 'file',
'#title' => t('Upload Main Image'),
'#default_value' => $node->image,
'#size' => 48,
'#description' => t('Upload Main Image.')
);
I have this in my form, it generates an image upload box. However the image isn't uploading and the name of the file isn't going into the database.
How can i process to this to
a) Get the file name to input into my database
b) Actually make this upload this file to root/files
Incase it is needed the full module can be found below.
<?php
// $Id$
/**
* @file
* Provides a "portfolio" node type.
*/
/**
* Implementation of hook_node_info().
*/
function portfolio_node_info() {
// We return an array since a module can define multiple node types.
// We're only defining one node type, type 'portfolio'.
return array(
'portfolio' => array(
'name' => t('Portfolio'), // Required.
'module' => 'portfolio', // Required.
'description' => t('Add a portfolio item!'), // Required.
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('Portfolio'),
'locked' => TRUE
)
);
}
/**
* Implementation of hook_menu().
*/
function portfolio_menu($may_cache) {
$items = array();
// Do not cache this menu item during the development of this module.
if (!$may_cache) {
$items[] = array(
'path' => 'node/add/portfolio',
'title' => t('Portfolio'),
'access' => user_access('create portfolio'),
);
}
return $items;
}
/**
* Implementation of hook_perm().
*/
function portfolio_perm() {
return array('create portfolio', 'edit own portfolio');
}
/**
* Implementation of hook_access().
*/
function portfolio_access($op, $node) {
global $user;
if ($op == 'create') {
return (user_access('create portfolio'));
}
if ($op == 'update' || $op == 'delete') {
return (user_access('edit own portfolio') && ($user->uid == $node->uid));
}
}
/**
* Implementation of hook_form().
*/
function portfolio_form($node) {
// Get metadata for this node type
// (we use it for labeling title and body fields).
// We defined this in portfolio_node_info().
$form['#attributes']['enctype'] = 'multipart/form-data';
$type = node_get_types('type', $node);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5
);
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#default_value' => $node->body,
'#rows' => 7,
'#required' => TRUE
);
$form['body_filter']['filter'] = filter_form($node->format);
$form['image_small'] = array(
'#type' => 'file',
'#title' => t('Upload Teaser Picture'),
'#default_value' => $node->image_small,
'#size' => 48,
'#description' => t('Upload Teaser Picture.')
);
$form['image'] = array(
'#type' => 'file',
'#title' => t('Upload Main Image'),
'#default_value' => $node->image,
'#size' => 48,
'#description' => t('Upload Main Image.')
);
return $form;
}
/**
* Implementation of hook_validate().
*/
function portfolio_validate($node) {
// Enforce a minimum word length of 3.
}
/**
* Implementation of hook_insert().
*/
function portfolio_insert($node) {
db_query("INSERT INTO {portfolio} (nid, vid, image_small, image) VALUES (%d, %d, '%s', '%s')",
$node->nid, $node->vid, $node->image_small, $node->image);
}
/**
* Implementation of hook_update().
*/
function portfolio_update($node) {
if ($node->revision) {
portfolio_insert($node);
}
else {
db_query("UPDATE {portfolio} SET image_small = '%s', image = '%s' WHERE vid = %d",
$node->image_small, $node->image, $node->vid);
}
}
/**
* Implementation of hook_delete().
*/
function portfolio_delete(&$node) {
// Delete the related information we were saving for this node.
db_query('DELETE FROM {portfolio} WHERE nid = %d', $node->nid);
}
/**
* Implementation of hook_load().
*/
function portfolio_load($node) {
return db_fetch_object(db_query('SELECT image_small, image FROM {portfolio} WHERE vid = %d',
$node->vid));
}
Comments
Whe are you not using CCK
Whe are you not using CCK for this purpose?
-----------------------------------------
CompuBase, websites and webdesign
node images
after wresting with this exact problem for several sites and never finding a straight-forward mechanism, i have recently been using the node images module to attach images to a node. in combination with the upload module, this adds an 'images' tag to each node. it saves very basic info about the file in a minimal mysql table much like the upload module.
while i have no doubt that cck can easily accomplish this task, i haven't made the leap because i like the specificity and control of using a custom module.
best of luck with your drupal site.
Mark Sanders
Q Collective
Mark Sanders
Q Collective
Image Field and Image Cache
Despite what you think, ImageField, ImageCache, CCK and Contemplate do exactly what you're looking for.
description and weight
i just tried out imagefield and cck and there are a couple of things that they don't do that are helpfully included in node_image. there is no way to assign weight to the images and control the order they are output. there is also no description field or the like for inputting a caption to be displayed near the image.
are there any other ways in cck / imagefield to do this?
Mark Sanders
Q Collective
Mark Sanders
Q Collective
It is not a leap,
It is not a leap, really.
Try it, it is simple and straight forward! It saves you a lot of work!
Check out the views module as well. They work very nice together.
-----------------------------------------
CompuBase, websites and webdesign
I am searching for an answer
I am searching for an answer to this, too.
And I don't want to use cck and views for some reasons. Anyone with an answer other then "use cck"?
Thanks
Too fast
I found the solution here:
http://api.drupal.org/api/file/developer/examples/fileupload.module/6/so...
Cheers Rainer
Not working yet
this is not working
Module examples/fileupload.module claims to work in Drupal 6, however it doesn't even compile in D6
http://drupal.org/node/246215
_
_