我正在尝试从表单上传文件,然后将其保存在位于web/uploads下的目录uvis_front_user_files中,但是当我保存表单时,一切顺利,但文件不在目标目录中。
甚至文件名也保存在数据库中,但在uvis_front_user_files目录中什么都没有。
表格:
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="heading heading-tertiary heading-border text-center">
<h4 class="heading-tertiary"><?php echo $quiz['title'] ?></h4>
<p><?php echo $quiz['description'] ?></p>
</div>
<?php if ($sf_user->hasFlash('success')): ?>
<div class="alert alert-success">
<?php echo $sf_user->getFlash('success'); ?>
</div>
<?php endif; ?>
<div class="col-md-6 col-md-offset-3">
<form method="POST" action="" enctype="multipart/form-data">
<?php echo $form->renderGlobalErrors(); ?>
<table class="sample-order-frm" width="100%">
<?php echo $form['name']->renderRow() ?>
<?php echo $form['email']->renderRow() ?>
</tr>
</table>
<?php $current = current($quiz['questions']) ?>
<?php if ($current["text"] !== NULL): ?>
<h4 class="heading-tertiary">Please leave blank the questions that do not apply:</h4>
<ol class="survey-ques-list">
<?php foreach ($quiz['questions'] as $questionId => $question): ?>
<li>
<label><?php echo $question['text']; ?></label>
<?php echo $form['question_'.$questionId]->render(); ?>
<?php echo $form['path']->render(); ?>
<?php echo $form['question_'.$questionId]->renderError(); ?>
</li>
<?php endforeach; ?>
</ol>
<br />
<?php endif; ?>
<?php echo $form->renderHiddenFields(false); ?>
<div class="text-center">
<input type="submit" id="submit" name="submit" value="Submit" class="btn btn-tertiary btn-lg"/>
</div>
</form>
</div>
</div>
</div>
</div>
行动文件:
<?php
/**
* uvisQuiz actions.
*
* @package we.com
* @subpackage uvisQuiz
* @version SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
*/
class uvisQuizActions extends sfActions {
/**
* Executes index action
*
* @param sfRequest $request A request object
*/
public function executeIndex(sfWebRequest $request) {
$this->forward404Unless($slug = $request->getParameter('slug'));
$this->forward404Unless($this->quiz = UvisQuizFrontPeer::getQuizBySlug($slug));
$this->form = new UvisQuizCustomForm(null, array('questions' => $this->quiz['questions']));
if($request->isMethod('POST')){
$params = $request->getParameter($this->form->getName());
$this->form->bind($params, $request->getFiles($this->form->getName()));
//$this->form->bind($params);
if($this->form->isValid()){
$uvisQuizUserObj = UvisQuizFrontPeer::saveQuiz($this->quiz, $params);
$this->getUser()->setFlash('completed_video_survey', true);
$this->redirect('@uvis_quiz_complete?slug='.$request->getParameter('slug'));
}
}
}
public function executeComplete(sfWebRequest $request) {
$this->forward404Unless($slug = $request->getParameter('slug'));
$this->forward404Unless($this->singleQuiz = UvisQuizFrontPeer::getQuizBySlug($slug));
$this->forward404Unless($this->getUser()->hasFlash('completed_video_survey'));
}
}
UvisQuizFrontPeer:
<?php
class UvisQuizFrontPeer extends BaseUvisQuizPeer {
public static function getQuizBySlug($slug) {
$criteria = new Criteria();
$criteria->addSelectColumn('uvis_quiz.id as id');
$criteria->addSelectColumn('uvis_quiz.title as title');
$criteria->addSelectColumn('uvis_quiz.description as description');
$criteria->addSelectColumn('uvis_quiz.path as path');
$criteria->addSelectColumn('uvis_quiz_question.id as question_id');
$criteria->addSelectColumn('uvis_quiz_question.question as question_text');
$criteria->addSelectColumn('uvis_quiz_question.answer_type as answer_type');
$criteria->addSelectColumn('uvis_quiz_question_answer.id as answer_id');
$criteria->addSelectColumn('uvis_quiz_question_answer.answer as answer_text');
$criteria->addJoin(self::ID, UvisQuizQuestionPeer::QUIZ_ID, Criteria::LEFT_JOIN);
$criteria->addJoin(UvisQuizQuestionPeer::ID, UvisQuizQuestionAnswerPeer::QUIZ_QUESTION_ID, Criteria::LEFT_JOIN);
$criteria->add(self::SLUG, $slug);
$criteria->add(self::DELETED_AT, null, Criteria::ISNULL);
$criteria->add(UvisQuizQuestionPeer::DELETED_AT, null, Criteria::ISNULL);
$criteria->add(UvisQuizQuestionAnswerPeer::DELETED_AT, null, Criteria::ISNULL);
$records = self::doSelectStmt($criteria)->fetchAll(PDO::FETCH_ASSOC);
$quizData = array();
foreach ($records as $record) {
if (!isset($quizData['id'])) {
$quizData['id'] = $record['id'];
$quizData['title'] = $record['title'];
$quizData['description'] = $record['description'];
$quizData['path'] = $record['path'];
$quizData['questions'] = array();
}
if (!isset($quizData['questions'][$record['question_id']])) {
$quizData['questions'][$record['question_id']]['text'] = $record['question_text'];
$quizData['questions'][$record['question_id']]['type'] = $record['answer_type'];
$quizData['questions'][$record['question_id']]['answer_choices'] = array();
$quizData['questions'][$record['question_id']]['answers'] = array();
}
if (!isset($quizData['questions'][$record['question_id']]['answers'][$record['answer_id']])) {
$quizData['questions'][$record['question_id']]['answer_choices'][$record['answer_id']] = $record['answer_text'];
$quizData['questions'][$record['question_id']]['answers'][$record['answer_id']]['text'] = $record['answer_text'];
}
}
return count($quizData) > 0 ? $quizData : false;
}
public static function saveQuiz($quizData, $params) {
$uvisQuizUserObj = new UvisQuizUser();
$uvisQuizUserObj->setQuizId($quizData['id']);
$uvisQuizUserObj->setName($params['name']);
$uvisQuizUserObj->setEmail($params['email']);
$uvisQuizUserObj->save();
foreach ($quizData['questions'] as $questionId => $question) {
if(isset($params['question_'.$questionId]) && $params['question_'.$questionId] != ''){
$uvisQuizUserAnswerObj = new UvisQuizUserAnswer();
$uvisQuizUserAnswerObj->setQuizUserId($uvisQuizUserObj->getId());
$uvisQuizUserAnswerObj->setQuiestionId($questionId);
if($question['type'] == 'free_text'){
$uvisQuizUserAnswerObj->setFreeTextAnswer($params['question_'.$questionId]);
$uvisQuizUserAnswerObj->setPath($params['path']);
}
else{
$uvisQuizUserAnswerObj->setAnswerId($params['question_'.$questionId]);
}
$uvisQuizUserAnswerObj->save();
}
}
return $uvisQuizUserObj;
}
}
测验自定义表格:
<?php
class UvisQuizCustomForm extends sfForm {
public function configure() {
$mimeTypes = array('application/pdf', 'application/x-pdf', 'application/rtf',
'application/vnd.oasis.opendocument.text', 'application/msword',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/x-msword', 'text/plain');
$this->widgetSchema['name'] = new sfWidgetFormInput(array(), array('class' => 'form-control'));
$this->widgetSchema['email'] = new sfWidgetFormInput(array(), array('class' => 'form-control'));
$this->widgetSchema['phone'] = new sfWidgetFormInput();
$this->validatorSchema['name'] = new sfValidatorString(array('max_length' => 64, 'required' => true, 'trim' => 'both'), array('required' => 'Name is required', 'max_length' => 'Maximum %max_length% characters allowed'));
$this->validatorSchema['email'] = new sfValidatorEmail(array('max_length' => 255, 'required' => true, 'trim' => 'both'), array('required' => 'Email is required', 'max_length' => 'Maximum %max_length% characters allowed', 'invalid' => 'Invalid email'));
$this->validatorSchema['phone'] = new sfValidatorRegex(array('pattern' => '/^[0-9\-]+$/', 'required' => false), array('invalid' => 'Invalid phone number'));
foreach ($this->options['questions'] as $questionId => $question) {
if($question['type'] == 'multiple'){
$this->widgetSchema['question_' . $questionId] = new sfWidgetFormChoice(array('choices' => $question['answer_choices'], 'expanded' => true));
$this->validatorSchema['question_' . $questionId] = new sfValidatorChoice(array('required' => false, 'choices' => array_keys($question['answer_choices'])), array('invalid' => 'Invalid option selected'));
} else {
$this->widgetSchema['path'] = new sfWidgetFormInputFile(array(
'label' => 'Upload File',
));
$this->setValidator('path', new sfValidatorFile(array(
'required' => false,
'path' => sfConfig::get('sf_upload_dir').'/uvis_front_user_files',
'mime_types' => $mimeTypes,
)));
$this->widgetSchema['question_' . $questionId] = new sfWidgetFormTextarea(array(), array('cols' => 50, 'rows' => 4));
$this->validatorSchema['question_' . $questionId] = new sfValidatorString(array('required' => false));
}
}
$this->widgetSchema['quiz_user_id'] = new sfWidgetFormInputHidden();
$this->validatorSchema['quiz_user_id'] = new sfValidatorPropelChoice(array('model' => 'UvisQuizUser', 'column' => 'id', 'required' => false));
$decorator = new sfWidgetFormSchemaFormatterMain($this->widgetSchema, $this->validatorSchema);
$this->widgetSchema->addFormFormatter('custom', $decorator);
$this->widgetSchema->setFormFormatterName('custom');
$this->widgetSchema->setNameFormat('UvisQuizFrontForm[%s]');
}
}