dro44817 2012-08-22 16:55
浏览 40
已采纳

Zend From文件上传不起作用

I'm having some issues uploading a file using Zend Framework. I have created a form, listed below, and have passed the data input to my model where I am attempting to upload the file. IT seems only to get the file name however and fails to upload to my uploads directory.

Form

<?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,
        ));

    }


}

Controller

<?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;
    }

}

Model

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'));

    }

}

  • 写回答

3条回答 默认 最新

  • douzhang7184 2012-08-23 13:52
    关注

    The issue was that I was accessing the form data before the upload with the following line:

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

    This is now placed after the upload in the model and the file data is accessed instead with the following:

    $file = $upload->getFileInfo();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历