This patch allows you to specify a subdirectory description of how you'de like the files to be saved. Handy for when different node types need to save the file to disk in different ways

The pathwill be appended to $_GLOBAL['file_directory_path'], mark it up with %username, %day, %dayname, %month, %monthname, %year . Blank if not required. Directories will be created, default (blank)

Example: somenodefiles/%year/%monthname/%username.

Will save as

$GLOBAL['file_directory_path']."/somenodefiles/2006/January/admin/somefile.pdf"

All filenames are preserved.

Comments

dgtlmoon’s picture

StatusFileSize
new3.08 KB

No idea if or where the patch i attached to this node went so here it is for good measure


--- flexinode-cvs/field_file.inc        2005-11-10 20:35:16.000000000 +1100
+++ flexinode/field_file.inc    2006-03-29 14:17:06.000000000 +1100
@@ -15,6 +15,7 @@
     '#required' => $field->required,
     '#weight' => $field->weight,
     );
+
   $form[$fieldname .'_old'] = array(
     '#type' => 'hidden',
     '#value' => serialize($node->$fieldname),
@@ -32,6 +33,7 @@
 }

 function flexinode_field_file_insert($field, $node) {
+
   $fieldname = 'flexinode_'. $field->field_id;
   $serialized = is_object($node->$fieldname) ? serialize($node->$fieldname) : '';
   db_query("INSERT INTO {flexinode_data} (nid, field_id, textual_data, serialized_data) VALUES (%d, %d, '%s', '%s')", $node->nid, $field->field_id, $node->$fieldname->filename, $serialized);
@@ -42,13 +44,61 @@
   $result = db_fetch_object(db_query('SELECT serialized_data FROM {flexinode_data} WHERE nid = %d AND field_id = %d', $node->nid, $field->field_id));
   $file = unserialize($result->serialized_data);
   if ($unconditional || $node->$fieldname != $file) {
-    file_delete($file->filepath);
   }
 }

+
+// replaces save path tags with correct values
+function _save_path_markup($metatag) {
+    global $user;
+
+    // add any extra functionality here, such as %workflowstate etc
+    foreach($metatag as $tag) {
+       switch(strtolower($tag)) {
+           case "%day":
+               $ret= date("j");
+           break;
+           case "%dayname":
+               return date("l");
+           break;
+           case "%month":
+               return date("n");
+           break;
+           case "%monthname":
+               return date("F");
+           break;
+           case "%year":
+               return date("Y");
+           break;
+           case "%username":
+               return $user->name;
+           break;
+
+       };
+    }
+
+}
+
+
 function flexinode_field_file_execute($field, $node) {
   $fieldname = 'flexinode_'. $field->field_id;
-  if ($file = file_save_upload($fieldname, variable_get('file_directory_path', NULL))) {
+  $savepath=variable_get('file_directory_path', NULL);
+  if(is_array($field->options)) {
+    $path_extention=preg_replace_callback("/%.*?(?=(\W|$))/","_save_path_markup",implode("",$field->options));
+    // create the subdirectories if path_extention is set
+    foreach(explode("/",$path_extention) as $p) {
+      if($p!="/") {
+        $extpath.="/$p";
+           if(!file_exists($savepath."/".$extpath)) {
+               mkdir($savepath."/".$extpath);
+           }
+       }
+    }
+    $savepath.=$extpath;
+  }
+
+
+  if ($file = file_save_upload($fieldname, $savepath )) {
     return $file;
   }
   elseif (empty($node->$fieldname)) {
@@ -70,6 +120,16 @@
   return unserialize($node->$fieldname);
 }

+function flexinode_field_file_config($field, $edit) {
+  return array('options' => array(
+      '#type' => 'textfield',
+      '#title' => 'Save path format',
+      '#default_value' => implode("",$field->options),
+      '#description' => t('Enter save path format, this will be appended to file_directory_path, mark it up with %username, %day, %dayname, %month, %monthname, %year . Blank if not required. Directories will be created, default (blank) <BR>Example: nodefiles/%year/%month/%username. All filenames are preserved.'),
+      ));
+}
+
+

 /**
  * @addtogroup themeable

Bèr Kessels’s picture

Version: » master
Priority: Normal » Critical

This looks extremely usefull to me. Can someone review this?

ahoeben’s picture

Priority: Critical » Normal
StatusFileSize
new10 KB

Patch works as advertised, but included needed cleanup. +1 for functionality

I moved the core functionality of the patch to flexinode.module, and rolled new patches for field_file.inc and field_image.inc. Image fields can now also set file save paths.

Attached tar contains three patches.

ahoeben’s picture

Status: Needs review » Needs work

I'll be rolling new patches since my patches above are kind-off sort-off bogus. Must sleep more...

ahoeben’s picture

StatusFileSize
new10.47 KB

patch 1 of 3

Patches flexinode.module with dgtlmoon's functionality. Adds '%userid' metapath element (user's uid). Usernames can e changed (by the user), at which time new directories would be created. I'm also not sure about special characters in the usernames. We'll probably have to add validation to the %username code.

ahoeben’s picture

StatusFileSize
new2.02 KB

patch 1 of 3

And again I screw up... Please disregard the previous patch
Here's the good patch for flexinode.module.

ahoeben’s picture

StatusFileSize
new2.97 KB

patch 2 of 3

Patches field_file to add the path directives. Also adds fields to optionally limit the filesize and extension (after upload).

ahoeben’s picture

StatusFileSize
new3.45 KB

patch 3 of 3

Patches field_image to add the path directives.

Note: please apply all three patches.

igrcic’s picture

can anyone please revert this for v 4.6 also ??

thank you so much

dgtlmoon’s picture

havent used 4.6 :( sorry

has this been commited to HEAD branch?

Bèr Kessels’s picture

Not committed. There are more pressing issues in queue for file fields.

igrcic’s picture

this is so much needed function! can anyone try to make path for 4.6.* ??? some directions maybe?? tnx

dgtlmoon’s picture

what errors do you get if you try to use it in 4.6?

Bèr Kessels’s picture

Note that I won't add new features on 4.6, 4.7 or any other stable branch. Also note that this feature is therefore marked for CVS.

dgtlmoon’s picture

Will this make it for 5.0? it is much needed

Bèr Kessels’s picture

please give the patch another review, using the latest HEAD file fields.

ahoeben’s picture

Title: Patch for flexinode file_field.inc to add configurable file save path directives » Configurable file save path directives file_field.inc and image_field.inc
Component: Code » flexinode.module (core)
StatusFileSize
new8.78 KB

Attached path is against 4.7.x 1.0, but needs some testing.

For file_field.inc, additional upload limitations were lifted from image_field.inc. A list of accepted extensions is a required option for the field, in order to increase security.

Bèr Kessels’s picture

Lets keep the focus and not mix up features.
This issue thread is about Configurable file save path directives file_field.inc and image_field.inc

I created another thread to allow file-extension settigns, find it in http://drupal.org/node/113506

ahoeben’s picture

In order to keep consistency between image and file uploading, implementing the two features became rather intertwined.