By decimal786 on
hi Drupal Champs
i want to add a file upload field to User Profile.i am working on Drupal 6 and using core Profile module for user profile.
I searched through the forums but didn't helped me out.
Do i need to customize the profile module or is there any other way to achieve this.Please don't suggest to use content profile and the CCk file field as i don't want the profiles as to be nodes.
Any other idea or suggestions most welcome.
Any kind of help will be really appreciated.
Thanks in Advance!!!!!!
James
Comments
i hope so.. else i'll have to do it myself
This would be a wonderful feature, though I'm not sure it will be added to the profile core. I will take a few hacks at it now, post progress..
you may change the
you may change the user.module and insert the form upload file
if (!$register){
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['document_upload'] = array(
'#type' => 'file',
'#title' => t('File'),
'#required' => FALSE,
'#description' => t('Click "Browse" to select a file to upload. The maximum file size per upload is %size MB.', array('%size' => round(file_upload_max_size()/(100000*100000), 0))),
'#weight' => -5,
);
$form['#validate'] = array('user_node_form_validate');
}
I added the if statement to check the user uid since it will be difficult to get the uid in the process of registration
then added the following function
function user_node_form_validate($form, &$form_state) {
if (isset($form['document_upload'])) {
$validators = array(
'file_validate_extensions' => array('txt')
);
print_r($forms);
$file = user_save_upload('document_upload', $validators);
if (!$file)
form_set_error('upload', 'You must select a valid file to upload.');
else {
$form_state['values']['title'] = $file->filename;
$form_state['values']['myfile'] = $file;
}
}
}
function user_save_upload($source, $validators = array(), $dest = FALSE, $replace = FILE_EXISTS_RENAME) {
global $user;
static $upload_cache;
// Add in our check of the the file name length.
$validators['file_validate_name_length'] = array();
// Return cached objects without processing since the file will have
// already been processed and the paths in _FILES will be invalid.
if (isset($upload_cache[$source])) {
return $upload_cache[$source];
}
// If a file was uploaded, process it.
if (isset($_FILES['files']) && $_FILES['files']['name'][$source] && is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
// Check for file upload errors and return FALSE if a
// lower level system error occurred.
switch ($_FILES['files']['error'][$source]) {
// @see http://php.net/manual/en/features.file-upload.errors.php
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
drupal_set_message(t('The file %file could not be saved, because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $source, '%maxsize' => format_size(file_upload_max_size()))), 'error');
return 0;
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
drupal_set_message(t('The file %file could not be saved, because the upload did not complete.', array('%file' => $source)), 'error');
return 0;
// Unknown error
default:
drupal_set_message(t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $source)), 'error');
return 0;
}
// Build the list of non-munged extensions.
// @todo: this should not be here. we need to figure out the right place.
$extensions = '';
foreach ($user->roles as $rid => $name) {
$extensions .= ' '. variable_get("upload_extensions_$rid",
variable_get('upload_extensions_default', 'jpg jpeg gif png txt html doc xls pdf ppt pps odt ods odp'));
}
// Begin building file object.
$file = new stdClass();
$file->filename = file_munge_filename(trim(basename($_FILES['files']['name'][$source]), '.'), $extensions);
$file->filepath = $_FILES['files']['tmp_name'][$source];
$file->filemime = file_get_mimetype($file->filename);
// Rename potentially executable files, to help prevent exploits.
if (preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
$file->filemime = 'text/plain';
$file->filepath .= '.txt';
$file->filename .= '.txt';
}
// If the destination is not provided, or is not writable, then use the
// temporary directory.
if (empty($dest) || file_check_path($dest) === FALSE) {
$dest = file_directory_temp();
}
$file->source = $source;
$file->destination = file_destination(file_create_path($dest .'/'. $file->filename), $replace);
$file->filesize = $_FILES['files']['size'][$source];
// Call the validation functions.
$errors = array();
foreach ($validators as $function => $args) {
array_unshift($args, $file);
$errors = array_merge($errors, call_user_func_array($function, $args));
}
// Check for validation errors.
if (!empty($errors)) {
$message = t('The selected file %name could not be uploaded.', array('%name' => $file->filename));
if (count($errors) > 1) {
$message .= '
';
}
else {
$message .= ' '. array_pop($errors);
}
form_set_error($source, $message);
return 0;
}
// Move uploaded files from PHP's upload_tmp_dir to Drupal's temporary directory.
// This overcomes open_basedir restrictions for future file operations.
$file->filepath = $file->destination;
if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->filepath)) {
form_set_error($source, t('File upload error. Could not move uploaded file.'));
watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->filepath));
return 0;
}
// If we made it this far it's safe to record this file in the database.
//print_r($user);
//print_r($user->uid);
$file->uid = $user->uid;
$file->status = FILE_STATUS_TEMPORARY;
$file->timestamp = time();
drupal_write_record('files', $file);
// Add file to the cache.
$upload_cache[$source] = $file;
return $file;
}
return 0;
}
this will upload to the temp file depending on where you point the files the rest is up to you to use copy files. I try to look for an existing module but there are no available so far for drupal 6
cheers
Thank you very much! Is there
Thank you very much!
Is there any article where you studied this?
Thank you too
I just add the function from different module particularly the upload image and combined them to the other modules. Sorry I dont have any formal studies on this but if you have any question just post them. I'll be happy to help. Happy coding
Where to add code?
I'd like to include a file upload during registration, and this seems like it is one solution. However, I'm a little unclear about how to implement it.
Where do I put your code in the "./modules/user/user.module"? Anywhere, or is there a line number? More to it than what I think?
Thanks!
@Quevin — Sr. Technical PM
I also was not able to get
I also was not able to get this working. I appreciate the work, since I haven't seen a workable solution for this use case, until now.
Sorry
Im so sorry for not able to answer your inquiry in time.. can you post the code so I can check what went wrong..
Thanks
edgar
Thank you
Thanks for sharing wonderful code, I also want to put file upload field in user registration form. I guess instead of changing any core modules one should look at other alternative of it. I think this code can be used when we override module which is good pratice to achieve things.
I have a module that will do
I have a module that will do this, waiting on someone with a CVS account to upload it. I'll link it back here once its up.
cant wait
Cant wait to see it animelon.. great to have you guys share your code with us.. happy coding :)
Put the module so we can review it here
Hi Animelion,
I do have a CVS account but before, upload the module let the community to test it during 2 or three weeks, then we can upload it as a formal module.
What do you think?
Julian
Sounds Good
The person with the CVS account who was supposed to upload it never did, so here it is.
It still needs a lot of work, but at the very least it allows file uploads during registration.
http://www.mediafire.com/?m2yot2zil2fnzez
julianmancera, should I be posting this somewhere else as well?
Sorry,module is not working perfectly.Help me,thanks in advance.
Hi animelion ,
After implementing your module,i have faced an error given below.
* The selected file c:/wamp/tmp/link_2.txt could not be uploaded, because the destination is not properly configured.
* Error occured while saving the image!
Images are uploaded in c:/wamp/tmp/ but not in my desired location ie indusnet/sites/default/files,where indusnet is our drupal root directory.
Hi Chiranjit.. Think it might help you..
you should change the "include_path = " in php.ini file located at C:\xampp\apache\bin\php.ini
Try it..
I'm requesting others to help Chiranjit.. if possible..
Thank You
I think there must occur severe problems if you change php.ini settings.So,try to give better options.
Looks like it finally got
Looks like it finally got uplaoded, and now it's just awaiting approval.
#820826: beanluc [beanluc]
Thanks
Hye
This is really help full for new drupal developer.
Thanks
BRIZ
Thanks,That many support
Thanks,
That many support the above the code.
but it is best way to used the in custom module
but i have used the code in in the form_alter on load registration form
if (!$register){
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['document_upload'] = array(
'#type' => 'file',
'#title' => t('File'),
'#required' => FALSE,
'#description' => t('Click "Browse" to select a file to upload. The maximum file size per upload is %size MB.', array('%size' => round(file_upload_max_size()/(100000*100000), 0))),
'#weight' => -5,
);
$form['#validate'] = array('user_node_form_validate');
}
and checked the validate condition in my function..
Aqueel