View source
<?php
function _filebrowser_build_new_upload_file_name($node, $form_state, $i) {
$file_name = $_FILES['files']['name']["file_{$i}"];
if (!empty($form_state['values']["file_name_{$i}"])) {
$path_info_new = pathinfo($form_state['values']["file_name_{$i}"]);
$path_info_old = pathinfo($file_name);
$file_name = $path_info_new['filename'];
if (isset($path_info_old['extension'])) {
$file_name .= ".{$path_info_old['extension']}";
}
}
_filebrowser_load_files($node);
$target = _filebrowser_convert_to_fs_encoding($node, $node->file_listing['.']['full-path'] . "/" . $file_name);
return $target;
}
function filebrowser_form_upload($form_state, $node) {
$form = array();
$form['filebrowser_uploads'] = array(
'#type' => 'fieldset',
'#title' => t('File Upload'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Uploaded file will be saved to the current directory.'),
'#prefix' => '<div class="attachments">',
'#suffix' => '</div>',
'#weight' => 30,
);
$form['#node'] = $node;
$form['#attributes'] = array(
'enctype' => "multipart/form-data",
);
_filebrowser_load_files($node);
$form['#submit'][] = 'filebrowser_form_upload_submit';
$form['#validate'][] = 'filebrowser_form_upload_validate';
$i = 1;
$form['filebrowser_uploads']["file_{$i}"] = array(
'#type' => 'file',
'#title' => t('Upload file'),
'#size' => 40,
);
$form['filebrowser_uploads']["description_{$i}"] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#size' => 255,
);
$form['filebrowser_uploads']["file_name_{$i}"] = array(
'#type' => 'textfield',
'#description' => t('Just put filename with NO EXTENSION here if you want to rename the file you want to upload'),
'#title' => t('New name'),
'#size' => 40,
);
$form['submitted'] = array(
'#tree' => TRUE,
);
$form['filebrowser_uploads']['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
);
$form['#redirect'] = NULL;
return $form;
}
function filebrowser_form_upload_validate($form, $form_state) {
$node = $form['#node'];
_filebrowser_load_files($node);
$i = 1;
$target = _filebrowser_build_new_upload_file_name($node, $form_state, $i);
if (!$node->folder_uploads->allow_overwrite && file_exists($target)) {
form_error($form['filebrowser_uploads']["file_{$i}"], t("This file already exists."));
}
if (!empty($node->folder_uploads->accepted_uploaded_files) && !_filebrowser_match_path($target, $node->folder_uploads->accepted_uploaded_files)) {
form_error($form['filebrowser_uploads']["file_{$i}"], t("Sorry, you can't upload this kind of file."));
}
}
function filebrowser_form_upload_submit($form, &$form_state) {
$i = 1;
$node = $form['#node'];
$target = _filebrowser_build_new_upload_file_name($node, $form_state, $i);
$success = copy($_FILES['files']['tmp_name']["file_{$i}"], $target);
if (!$success) {
drupal_set_message(t("Unable to upload this file, do you have filesystem right to do that ?"), 'error');
}
else {
_filebrowser_load_files($node, NULL, TRUE);
if (!empty($form['filebrowser_uploads']["description_{$i}"])) {
$file = $node->file_listing[_filebrowser_safe_basename($target)];
module_invoke_all('filebrowser_metadata_set', $file, array(
'description' => $form_state['values']["description_{$i}"],
));
}
}
drupal_goto("node/{$node->nid}/{$node->file_listing['.']['fid']}", _filebrowser_url_query());
}