我正在使用zend表单上传图片。
我的问题是在表单中编辑信息时,表单附带了所有信息,但是 file_path 为空,因此当您单击提交发生的更新时,将 file_path 设置为 null。
我只想在加载时查看可编辑项目的路径,因此当您单击提交时,旧路径将在那里。
public function EditaddAction() {
$session = new Zend_Session_Namespace('user');
$userid = $session->id;
// in case the request was an edit request
$id = (int) $this->_request->getParam('id');
//The incoming request
$request = $this->getRequest();
//initialize form
$form = new Admin_Form_Banner();
//uploaded file settings
$file = $form->file_path;
$file->setDestination(ZendX_Image::getFullUploadPath() . '/files/get/original/');
//instance of db
$db = Zend_Db_Table::getDefaultAdapter();
if ($this->getRequest()->isPost()) {
if ($form->isValid($request->getPost())) {
$dbFilePath = "/files/get/original/" . $file->getFileName(null, false);
//code to get the duration of the video
$sourceVideo = PUBLIC_PATH . $form->getValue('file_path');
ob_start();
passthru("ffmpeg -i \"" . $sourceVideo . "\" 2>&1");
$duration = ob_get_contents();
ob_end_clean();
preg_match('/Duration: (.*?),/', $duration, $matches);
$duration = $matches[1];
$duration_array = preg_split('[:]', $duration);
$duration = $duration_array[0] * 3600 + $duration_array[1] * 60 + $duration_array[2];
die($time);
end of code to get the duration of the video
if (isset($id) && $id != "" && $file->receive()) {
try {
$db->update('banner', array('banner_title' => $form->getValue('banner_title'),
'banner_type' => $form->getValue('banner_type'),
'banner_position' => $form->getValue('banner_position'),
'banner_link' => $form->getValue('banner_link'),
'link_open' => $form->getValue('link_open'),
'file_path' => $dbFilePath,
'is_active' => $form->getValue('is_active')
), array('id =?' => $id));
$this->flash('Banner Updated', 'admin/banner');
} catch (Exception $e) {
$this->flash($e->getMessage(), 'admin/banner');
}
} else {
try {
$db->insert('banner', array('banner_title' => $form->getValue('banner_title'),
'banner_type' => $form->getValue('banner_type'),
'banner_position' => $form->getValue('banner_position'),
'banner_link' => $form->getValue('banner_link'),
'link_open' => $form->getValue('link_open'),
'file_path' => $dbFilePath,
'is_active' => $form->getValue('is_active'),
'created_by' => $userid,
'date_ceated' => date('Y/m/d H:i:s'),
'is_deleted' => 0,
));
$this->flash('Banner Added', 'admin/banner');
} catch (Exception $e) {
$this->flash($e->getMessage(), 'admin/banner');
}
}
}
}
if (isset($id) && $id != "") {
$checkvalues = $db->fetchCol($db->select()->from(array('banner'),array('file_path')));
$values = $db->fetchRow("SELECT * FROM banner WHERE id = ?", $id);
$values['file_path'] = $checkvalues;
$form->populate($values);
}
$this->view->form = $form;