Zend-Image在错误的目的地上传

时间:2012-03-19 16:21:51

标签: zend-framework

请帮助..我是新手并尝试学习Zend Framework,但在上传图片时遇到问题。 Application.ini文件中的脚本

uploads.uploadPath = APPLICATION_PATH "/../public/uploads"

上传图片的表单:

$this->setAction('/data/personil/create')
        ->setMethod('post');

    //Item input untuk gambar
    $images = new Zend_Form_Element_File('images');
    $images->setMultiFile(3)
    ->addValidator('IsImage')
    ->addValidator('Size', false, '204800')
    ->addValidator('Extension', false, 'jpg,png,gif')
    ->addValidator('ImageSize', false, array(
            'minwidth'  => 150,
            'minheight' => 150,
            'maxwidth'  => 700,
            'maxheight' => 700
    ))
    ->setValueDisabled(true);           
    // attach element to form
    $this->addElement($images); 

    // create display group for file elements
    $this->addDisplayGroup(array('images'), 'files');
    $this->getDisplayGroup('files')
    ->setOrder(40)
    ->setLegend('Images');

控制器动作创建:

public function createAction()
{
    $form = new Pengadilan_Form_PersonilCreate();
    $this->view->form = $form;
    $flashMessenger = $this->_helper->FlashMessenger;

    //SImpan kedatabase
    if($this->getRequest()->isPost()){
        if($form->isValid($this->getRequest()->getPost())){
            $personil = new Pengadilan_Model_Personil();
            $personil->fromArray($form->getValues());
            $personil->RecordDate = date('Y-m-d', mktime());
            $personil->DisplayStatus = 0;
            $personil->DisplayUntil = null;
            $personil->save();
            $id = $personil->RecordId;

            $config = $this->getInvokeArg('bootstrap')->getOption('uploads');
            $form->images->setDestination($config['uploadPath']);
            $adapter = $form->images->getTransferAdapter();
            for($x=0; $x<$form->images->getMultiFile(); $x++) {
                $xt = @pathinfo($adapter->getFileName('images_'.$x.'_'), PATHINFO_EXTENSION);
                $adapter->clearFilters();
                $adapter->addFilter('Rename', array(
                        'target' => sprintf('%d_%d.%s', $id, ($x+1), $xt),
                        'overwrite' => true
                ));
                $adapter->receive('images_'.$x.'_');

            }


             $this->_helper->getHelper('FlashMessenger')->addMessage('Data sekses diinput ke database #' . $id . '. Admin akan segera merivew, jika diterima, akan ditampilkan dalam waktu 48 jam, Terima kasih.');
    $this->_redirect('/data/personil/sukses');
        }
    }

}

控制器操作显示:

public function displayAction()
{
    //Pertama setting filters
    $filters  = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
            );
    $validators = array(
            'id' => array('NotEmpty', 'Int')
            );
    $input = new Zend_Filter_Input($filters, $validators);
    $input->setData($this->getRequest()->getParams());      
    if($input->isValid()){
        $q = Doctrine_Query::create()
            ->from('Pengadilan_Model_Personil p')
            ->leftJoin('p.Pengadilan_Model_Jabatan j')
            ->leftJoin('p.Pengadilan_Model_Tupoksi t')
            ->leftJoin('p.Pengadilan_Model_Golongan g')             
            ->leftJoin('p.Pengadilan_Model_Agama a')
            ->leftJoin('p.Pengadilan_Model_Kelamin k')
            ->where('p.RecordId = ?', $input->id)
            ->addWhere('p.DisplayStatus = 1')
            ->addWhere('p.DisplayUntil >= CURDATE()');


        $result = $q->fetchArray();
        if(count($result) == 1){
            $this->view->personil = $result[0];
            $this->view->images = array();
            $config = $this->getInvokeArg('bootstrap')->getOption('uploads');
            foreach (glob("{$config['uploadPath']}/{$this->view->item['RecordID']}_*") as $file) {
                $this->view->images[] = basename($file);

    }
        }else{
            throw new Zend_Exception('Maaf, halaman tidak ditemukan, 404');
        }
    }else{
        throw new Zend_Exception('Kesalahan Input');
    }

}

查看的最后一个脚本:display.phtml

  <div id="images">
<?php foreach ($this->images as $image): ?>
<img src="/uploads/<?php echo $this->escape($image); ?>" width="150" height="150" />    
<?php endforeach; ?>

图片上传配置目的地为/ public / uploads 在我的案例中,/ public

图片成功上传和重命名但在目录外 图像不会显示在display.phtml

非常感谢你的帮助..

1 个答案:

答案 0 :(得分:0)

首先,您需要设置表单编码File element link
$form->setAttrib('enctype', 'multipart/form-data');

此外,您可能应该在发布表单之前完成设置image元素。因此,将setDestination内容移动到初始化表单的位置,您可能更有可能将其工作。

public function createAction()
{
    $form = new Pengadilan_Form_PersonilCreate();

    //set form encoding
    $form->setAttrib('enctype', 'multipart/form-data');

    //get path and set destination for image element
    $config = $this->getInvokeArg('bootstrap')->getOption('uploads');
    $form->images->setDestination($config['uploadPath']);

    $this->view->form = $form;
    //consider intializing flash messenger in the init() method
    $flashMessenger = $this->_helper->FlashMessenger;

    //SImpan kedatabase
    if($this->getRequest()->isPost()){
        if($form->isValid($this->getRequest()->getPost())){
            //more code...

            $adapter = $form->images->getTransferAdapter();
            for($x=0; $x<$form->images->getMultiFile(); $x++) {
                $xt = @pathinfo($adapter->getFileName('images_'.$x.'_'), PATHINFO_EXTENSION);
                $adapter->clearFilters();
                $adapter->addFilter('Rename', array(
                        'target' => sprintf('%d_%d.%s', $id, ($x+1), $xt),
                        'overwrite' => true
                ));
                $adapter->receive('images_'.$x.'_');

            }

             $this->_helper->getHelper('FlashMessenger')->addMessage('Data sekses diinput ke database #' . $id . '. Admin akan segera merivew, jika diterima, akan ditampilkan dalam waktu 48 jam, Terima kasih.');
             $this->_redirect('/data/personil/sukses');
        }
    }

}

不确定这是否会解决所有问题,但它应该是一个开始......