2009-10-29 32 views
2

我正在爲Drupal站點創建批量上傳功能。使用閃光燈,我可以將文件上傳到特定的網址,然後處理這些文件。我想要做的不僅僅是上傳文件,而是創建一個特定類型的節點,並將文件保存到已經使用CCK設置的文件區中。由於這些是音頻文件,因此filefield處理文件很重要,因此可以使用getid3模塊提供附加元數據。用文件域保存節點

現在我瀏覽了一些代碼,因爲我無法找到API文檔,但是根本不清楚我應該如何處理這個問題。理想情況下,我可以將該文件傳遞給一個函數,並僅使用保存節點時返回的數據,但我一直無法找到該函數。

如果任何人有這方面的經驗,我會apreciate如何處理這個問題的一些指針。

回答

8

幾周前我不得不做類似的事情,最後調整了Remote File module的一些功能,特別是remote_file_cck_attach_file()功能。它使用filefield模塊中的field_file_save_file()函數,這可能是您正在查找的函數。

就我而言,這些文件是從幾個遠程位置獲取的,並使用file_save_data()臨時存儲。它們連接到一個CCK的FileField上hook_nodeapi() presave問題,請使用下列內容:

public static function attachAsCCKField(&$node, $filepath, $fieldname, $index=0) { 
    // Grab the filefield definition 
    $field = content_fields($fieldname, $node->type); 
    $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field)); 
    $fieldFileDirectory = filefield_widget_file_path($field); 
    // This path does not necessarily exist already, so make sure it is available 
    self::verifyPath($fieldFileDirectory); 
    $file = field_file_save_file($filepath, $validators, $fieldFileDirectory); 
    // Is the CCK field array already available in the node object? 
    if (!is_array($node->$fieldname)) { 
    // No, add a stub 
    $node->$fieldname=array(); 
    } 
    $node->{$fieldname}[$index] = $file; 
} 

$filepath是路徑,應附文件,$fieldname是的FileField實例到節點和$index中使用的內部名稱在多個字段條目的情況下將是附加文件的基於0的索引。

該函數在實用程序類中結束,因此爲verifyPath()調用的類語法。通話只是確保目標目錄可用:

public static function verifyPath($path) { 
    if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) { 
    throw new RuntimeException('The path "' . $path . '" is not valid (not creatable, not writeable?).'); 
    } 
} 

這對我來說 - 所有其他事情發生在節點自動保存。

我還沒有使用getid3模塊,所以我不知道它是否會與這種方式一起玩。另外,我不需要在文件字段中添加額外的信息/屬性,所以也許你不得不把更多的信息放到字段數組中,而不僅僅是field_file_save_file()返回的文件。無論如何,希望這會有所幫助,祝你好運。

+1

爲field_file_save_file()+1,這將節省我一些時間。 – 2009-10-30 06:22:13

+0

看起來非常有希望,如果我沒有截止日期,我會深入挖掘並立即嘗試。 – googletorp 2009-10-30 14:56:34

1

我已經做了一些whith imagefield工作,我認爲結構必須是正確的,否則它將無法正常工作。它花了很多試驗和錯誤。這是我填充的imagefield。

$image['data'] =array(
      'title' => $media_reference_attributes->getNamedItem("source")->value, 
      'description' => $description, 
      'alt' => "",); 
$image['width'] = $width; 
$image['height'] = $height; 
$image['mimetype'] = $mime_type 
$image['uid'] = 1; 
$image['status'] = 1; 
$image['fid'] = $fid; 
$image['filesize'] = $file->filesize; 
$image['nid'] = $id; 
$image['filename'] = $url; 
$image['timestamp'] = $file->timestamp; 
$image['filepath'] = $file_path; 

希望這有些幫助。

1

如果您需要查看集成Flash上​​傳,您可能需要查看Image FUpload

要將文件推送到另一臺服務器,同時仍然通過Drupal處理它們聽起來有點像CDN空間,也許看看CDNCDN2項目中的行爲?

如果您發現一個清晰的解決方案,請回來貼出來!

+0

圖片FUpload看起來相似。然而,最大的區別是我想爲每個文件創建一個節點,所以看起來我不能像他們那樣做。請仔細看看它是否可以提供任何幫助。 – googletorp 2009-10-29 17:02:42