0

我在使用 Zend Framework 上传文件时遇到了一些问题。我创建了一个表单,如下所列,并将数据输入传递给我尝试上传文件的模型。IT 似乎只是获取文件名,但无法上传到我的上传目录。

形式

<?php

class Application_Form_Admin extends Zend_Form
{

    public function init()
    {
        // Set the method for the display form to POST
        $this->setMethod('post');

        // set the data format
        $this->setAttrib('enctype', 'multipart/form-data');

        // Add the title
        $this->addElement('file', 'ogimage', array(
            'label'      => 'Default App Image',
            'required'   => false
        ));

        // Add the submit button
        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Submit',
        ));

        // And finally add some CSRF protection
        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));

    }


}

控制器

<?php

class AdminController extends Zend_Controller_Action
{

    /**
     * @var The Admin Model
     *
     *
     */
    protected $app = null;

    /**
     * init function.
     *
     * @access public
     * @return void
     *
     *
     */
    public function init()
    {

        // get the model
        $this->app = new Application_Model_Admin();

    }

    /**
     * indexAction function.
     *
     * @access public
     * @return void
     *
     *
     */
    public function indexAction()
    {
        // get a form
        $request = $this->getRequest();
        $form    = new Application_Form_Admin();

        // pre populate form
        $form->populate((array) $this->app->configData());

        // handle form submissions
        if($this->getRequest()->isPost()) {

            if($form->isValid($request->getPost())) {

                // save the clips
                $this->app->saveConfig($form);

                // redirect
                //$this->_redirect('/admin/clips');

            }

        }

        // add the form to the view
        $this->view->form = $form;
    }

}

模型

class Application_Model_Admin
{

    /**
    * @var Bisna\Application\Container\DoctrineContainer
    */
    protected $doctrine;

    /**
    * @var Doctrine\ORM\EntityManager
    */
    protected $entityManager;

    /**
    * @var ZC\Entity\Repository\FacebookConfig
    */
    protected $facebookConfig;

    /**
     * Constructor
     */
    public function __construct(){

        // get doctrine and the entity manager
        $this->doctrine     = Zend_Registry::get('doctrine');
        $this->entityManager    = $this->doctrine->getEntityManager();

        // include the repository to get data
        $this->facebookConfig   = $this->entityManager->getRepository('\ZC\Entity\FacebookConfig');

    }


    /**
     * saveConfig function.
     * 
     * @access public
     * @param mixed $form
     * @return void
     */
    public function saveConfig($form){

        // get the entity
        $config = new \ZC\Entity\FacebookConfig();

        // get the values
        $values = $form->getValues();

        // upload the file
        $upload = new Zend_File_Transfer_Adapter_Http();
        $upload->setDestination(APPLICATION_PATH . '/../uploads/');
        try {
            // upload received file(s)
            $upload->receive();
        } catch (Zend_File_Transfer_Exception $e) {
            $e->getMessage();

        }

// get some data about the file
$name = $upload->getFileName($values['ogimage']);
$upload->setOptions(array('useByteString' => false));
//$size = $upload->getFileSize($values['ogimage']);
//$mimeType = $upload->getMimeType($values['ogimage']);
print_r('<pre>');var_dump($name);print_r('</pre>');
//print_r('<pre>');var_dump($size);print_r('</pre>');
//print_r('<pre>');var_dump($mimeType);print_r('</pre>');
die;
// following lines are just for being sure that we got data
print "Name of uploaded file: $name 
";
print "File Size: $size 
";
print "File's Mime Type: $mimeType";

// New Code For Zend Framework :: Rename Uploaded File
$renameFile = 'file-' . uniqid() . '.jpg';
$fullFilePath = APPLICATION_PATH . '/../uploads/' . $renameFile;

// Rename uploaded file using Zend Framework
$filterFileRename = new Zend_Filter_File_Rename(array('target' => $fullFilePath, 'overwrite' => true));
$filterFileRename->filter($name);

        // loop through the clips and add to object
        foreach($values as $k => $column){
            $config->__set($k, $column);
        }

        // save or update the clips object
        if(empty($values['id'])){
            $this->entityManager->persist($config);
        } else {
            $this->entityManager->merge($config);
        }

        // execute the query
        $this->entityManager->flush();

        // set the id
        $form->getElement('id')->setValue($config->__get('id'));

    }

}

4

3 回答 3

2

问题是我在上传之前使用以下行访问表单数据:

// get the values
$values = $form->getValues();

现在将其放置在模型中上传之后,并使用以下内容访问文件数据:

$file = $upload->getFileInfo();
于 2012-08-23T13:52:47.250 回答
1

您必须将文件移动到您想要的位置。这是我在代码中使用的一个小片段

if($form->isValid($request->getPost())) {
 $uploadedFile = new Zend_File_Transfer_Adapter_Http();
 $uploadedFile->setDestination(APPLICATION_PATH.'/../public_uploads/');
 if($uploadedFile->receive()) {
  //Code to process the file goes here 
 } else {
  $errors = $uploadedFile->getErrors();
 }
 $this->app->saveConfig($form);
}

希望这可以帮助您入门。

于 2012-08-22T17:37:43.357 回答
0

要完成这项工作,您需要setValueDisabled = true在表单元素中进行设置,否则一旦调用$form->getValues()它就会将文件上传到系统临时文件夹。

目前$form->getValues(),您甚至在设置目标之前就在调用,因此文件将变为默认值(系统温度)。

于 2012-08-23T09:59:40.800 回答