如何修改upload模組才能使上傳文件分目錄存儲?

請教如何修改upload模組,使得上傳的文件分目錄存儲?

現在通過upload模組上傳文件,全部都是存放在files目錄下的,這樣文件一多查找管理都很困難。

如何才能像wordpress一樣,按日期分目錄存儲?例如2007年10月3日上傳的文件,自動存放為形如www.website.com/files/2007/10/03/xxx.zip

我查看了upload模組的API(Drupal5 APIDrupal6 API),感覺應該可以通過修改某些程式碼實現(不想依賴第三方模組)。但我不太懂PHP,不知具體如何修改。希望哪位老大指點一下。

jimmy 的照片

Re: 如何修改upload模組才能使上傳文件分目錄存儲?

可以參考一下imagefield這個模組的code
應該會有點幫助...

--
from open mind to open source~

yelban 的照片

Re:

可以考慮使用 uploadpath 模組
http://drupal.org/project/uploadpath
此模組需搭配 token 模組使用
http://drupal.org/project/token

可以使用 token 的變數作為上傳目錄的前綴
在管理後台
設定 Pattern for the file prefix 即可
例如 [type-name]
或者 [yyyy]

這樣原本上傳的檔案路徑
files/xp-bra.zip
就會變成
files/2007/xp-bra.zip (使用 [yyyy])

只不過若要做到 2007/10/11 之類的上傳路徑
可能需要修改 uploadpath.module line 67
token_replace
改成
token_replace_multiple
但是這樣還要在 uploadpath.module 對 variable_get('uploadpath_prefix', '') 另外做處理

如果出現 date() expects parameter 2 to be long 錯誤訊息
需要自己做一些patch 動作
http://drupal.org/node/153737

tky 的照片

Re: 如何修改upload模組才能使上傳文件分目錄存儲?

原來token是這樣用的啊!
一直搞不清楚那玩意是幹嘛用的。

想順便問一下,有沒有人知道怎麼樣讓使用者透過IMCE的介面可以在自己的personal file資料夾中新增子資料夾啊?
這樣比較方便管理說。

tky

Cartson@drupal.org 的照片

Re:

jimmy推荐的imagefield模組的code我沒怎么看懂,倒是uploadpath模組code很簡單。

參考了uploadpath模組的code,我稍作修改,直接將其中一部分移植到了upload模組中。
基本實現了我期望的功能,且不需要搭配 token 模組就可以使用。

下面是修改移植的code,放置于upload模組upload_nodeapi()中 line 475

    // *** Upload files saved in directory by date *** CODE form uploadpath module!!!
    case 'submit':
      if (isset($node->files)) {
        foreach ($node->files as $key => $file) {
          if (0 === strpos($key, 'upload_')) {  // Only rewrite the name when adding the file, not when updating it
            // Get the new, prefixed file name
            // Set the timezone you want
            $timezone  = +8;
            $file_name = str_replace(array(' ', "\n", "\t"), '_', gmdate("Y/M/d/", time() + 3600*($timezone+date("I")))) . $node->files[$key]['filename'];

            // Create the directory if it doesn't exist yet.
            $dirs = explode('/', dirname($file_name));
            $directory = file_directory_path();
            while (count($dirs)) {
              $directory .= '/' . array_shift($dirs);
              file_check_directory($directory, FILE_CREATE_DIRECTORY);
            }
            // Change where the file will be saved to the specified directory.
            $node->files[$key]['filename'] = $file_name;
          }
        }
      }
      break;

其中下面的code是我修改過得
對此code再做修改應該可以實現設定其他目錄存放的方式,例如按使用者名新建目錄存放

            // Get the new, prefixed file name
            // Set the timezone you want
            $timezone  = +8;
            $file_name = str_replace(array(' ', "\n", "\t"), '_', gmdate("Y/M/d/", time() + 3600*($timezone+date("I")))) . $node->files[$key]['filename'];

但有個問題
上傳預覽時顯示的目錄還是原來的樣子,

但發表保存后,就正常了。

附加檔案大小
NewUpload_b.png67.55 KB
NewUpload_a.png53 KB
yelban 的照片

Re:

上傳預覽時顯示的目錄還是原來的樣子

是的, uploadpath + token 也有這種情形
是因為目錄改寫這行為是在 node submit 的時候才花生的

Cartson@drupal.org 的照片

Re: 如何修改upload模組才能使上傳文件分目錄存儲?

那么有什么修改方法可以讓上傳預覽時就顯示正確的目錄?

jimmy 的照片

Re: 如何修改upload模組才能使上傳文件分目錄存儲?

你們的資訊真讚,收錄起來~

--
from open mind to open source~