2012-09-20 29 views
1

我正在使用SilverStripe 2.4.7,我想添加一個方法來解析我剛剛上傳的FileIFrameField文件。讓我難過的東西是放在哪裏。我正在考慮onAfterWrite方法,但只有在其他字段第一次保存後纔會上傳文件,因此我不確定這會起作用。帶解析的FileIFrameField

我的問題是:這種事情的最佳做法是什麼?

編輯

我有這行代碼其中$ filename是我上傳的文件的路徑,但我不斷收到一個「沒有這樣的文件或目錄錯誤」。我甚至在文件路徑中嘗試過硬編碼,但得到相同的錯誤。

$fh = fopen($filename, 'r'); 
+0

所以你在談論一個sitetree對象,或一個普通的數據對象,通過complextablefield進行管理?行爲是不同的,我認爲,因爲'網頁'被立即保存到分支機構 – schellmax

+0

編號的數據庫。對不起,我遺漏了大量的信息。我正在使用DataObjectManager,並且其中的一個DataObjecta具有此上傳字段。 – MillyMonster

+0

你能否更詳細地描述你想要用這個文件做什麼,我看不到你要做什麼,所以很難說它在哪裏做。 – Zauberfisch

回答

1

解析一個新的文件將是掛接到uploadfield保存方法的最佳途徑,爲FileIframeField你可以做,按分分級,並覆蓋保存()

(在SilverStripe 3有一個名爲UploadField新的類,在UploadField你需要覆蓋UploadField->upload(SS_HTTPRequest $request),文件會有accesable這樣的:$tmpfile = $request->postVar($this->getName());

下方,例如在如何做到這一點的FileIframeField:

class myFileIFrameField extends FileIFrameField { 
    public function save($data, $form) { 
     if (
      !isset($data['FileSource']) 
      || ($data['FileSource'] == 'new' && (!isset($_FILES['Upload']) || !$_FILES['Upload'])) 
      || ($data['FileSource'] == 'existing' && (!isset($data['ExistingFile']) || !$data['ExistingFile'])) 
     ) { 
      $form->sessionMessage(_t('FileIFrameField.NOSOURCE', 'Please select a source file to attach'), 'required'); 
      Director::redirectBack(); 
      return; 
     } 
     $fileContent = false; 
     if($data['FileSource'] == 'new') { 
      $fileContent = file_get_contents($_FILES['Upload']['tmp_name']); 
     } 
     elseif($data['FileSource'] == 'existing') { 
      $fileObject = DataObject::get_by_id('File', $data['ExistingFile']); 
      $fileContent = file_get_contents($fileObject->getFullPath()); 
     } 
     if ($fileContent) { 
      // parse the $fileContent here 
     } 
     // if you want to still save the file into a relation, 
     //meaning if you want to have the actually FileIframeField behaviour still in tact then call 
     return parent::save($data, $form); 
     // other wise, if you do not want to save the relation and you don't want to save the file to the server 
     // thenn do NOT call parent::save, just do: 
     // Director::redirectBack(); 
    } 
}