Zend form file upload error

For programming and general questions on Zend Framework

Zend form file upload error

Postby davidtheo on Thu Oct 11, 2012 12:46 pm

HI All

I have a small problem with the Zend_Form_Element_File class

When I comment out
Code: Select all
->setDestination(BASE_PATH."/../public/images")
it works fine, but when I uncomment this line I get the following error

Code: Select all
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in path/libzend-framework-php/Zend/Controller/Dispatcher/Standard.php:248 Stack trace: #0 path/libzend-framework-php/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 path/libzend-framework-php/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch() #2 path/libzend-framework-php/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() #3 path/public/index.php(26): Zend_Application->run() #4 {main} Next exception 'Zend_Controller_Exception' with message 'Invalid controller specified (error)#0 path/libzend-framework-php/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 path/libzend-framework-php/Zend/Controller/Plugin/Broker.php on line 336



Zend form

Code: Select all
       
$this->setAction("/admin/main")->setMethod("post");
        $this->setAttrib('enctype', 'multipart/form-data');
              
        $image = new Zend_Form_Element_File('image');
       $image->setLabel('Image')
       ->setDestination(BASE_PATH."/../public/images")
       ->setRequired(false)
       ->addFilter('HtmlEntities')
       ->addFilter('StringTrim')
        ->addValidator('Count', false, 1)
        ->addValidator('Extension', false, 'jpg,jpeg,png,gif');
       
        $image_alt = new Zend_Form_Element_Text('image_alt');
       $image_alt->setLabel('Image alt text')
       ->setOptions(array('size' => '30'))
       ->setRequired(false)
        ->addValidator('Alnum')
       ->addFilter('HtmlEntities')
       ->addFilter('StringTrim');
       
     $submit = new Zend_Form_Element_Submit('submit');
       $submit->setLabel('Submit')
       ->setOptions(array('class' => 'submit'));
       
       $this->addElement($image)
        ->addElement($image_alt)
       ->addElement($submit);


Controller
Code: Select all
public function indexAction()
    {
        $form = new Admin_Form_Cvmain();
       $this->view->form = $form;
        $model = new model_name();
        if($this->getRequest()->isPost()){
            if($form->isValid($this->getRequest()->getPost())){
                $values = $form->getValues();
               
                if(empty($values['id'])){
                    $id = $model->insert($values);
                }else{
                    $id = $model->update($data);
                }
            }
        }
       
    }


Any Please help
davidtheo
 
Posts: 28
Joined: Fri Nov 11, 2011 11:41 pm

Re: Zend form file upload error

Postby us2rn4m2 on Thu Oct 11, 2012 9:22 pm

Hi,

In your modules/admin/controllers/ -> create an ErrorController + an error action
In your modules/admin/views/error/ -> create an error.phtml file

If it doesn't work create a default module (call 'default') and do the same thing like explain above

- Admin or Default modules
Code: Select all
<?php
class Default_ErrorController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
       
        if (!$errors || !$errors instanceof ArrayObject) {
            $this->view->message = 'You have reached the error page';
            return;
        }
       
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $priority = Zend_Log::NOTICE;
                $this->view->message = 'Page not found';
                break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $priority = Zend_Log::CRIT;
                $this->view->message = 'Application error';
                break;
        }
       
        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->log($this->view->message, $priority, $errors->exception);
            $log->log('Request Parameters', $priority, $errors->request->getParams());
        }
       
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }
       
        $this->view->request   = $errors->request;
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }


}


- error.phtml
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Zend Framework Default Application</title>
</head>
<body>
  <h1>An error occurred !</h1>
  <h2><?php echo $this->message ?></h2>

  <?php if (isset($this->exception)): ?>
 
  <h3>Exception information:</h3>
  <p>
      <b>Message:</b> <?php echo $this->exception->getMessage() ?>
  </p>

  <h3>Stack trace:</h3>
  <pre><?php echo $this->exception->getTraceAsString() ?>
  </pre>

  <h3>Request Parameters:</h3>
  <pre><?php echo $this->escape(var_export($this->request->getParams(), true)) ?>
  </pre>

  <?php endif ?>

</body>
</html>
us2rn4m2
 
Posts: 49
Joined: Mon Jun 18, 2012 12:41 am

Re: Zend form file upload error

Postby davidtheo on Thu Oct 25, 2012 3:18 pm

us2rn4m2 wrote:Hi,

In your modules/admin/controllers/ -> create an ErrorController + an error action
In your modules/admin/views/error/ -> create an error.phtml file

If it doesn't work create a default module (call 'default') and do the same thing like explain above

- Admin or Default modules
Code: Select all
<?php
class Default_ErrorController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
       
        if (!$errors || !$errors instanceof ArrayObject) {
            $this->view->message = 'You have reached the error page';
            return;
        }
       
        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                // 404 error -- controller or action not found
                $this->getResponse()->setHttpResponseCode(404);
                $priority = Zend_Log::NOTICE;
                $this->view->message = 'Page not found';
                break;
            default:
                // application error
                $this->getResponse()->setHttpResponseCode(500);
                $priority = Zend_Log::CRIT;
                $this->view->message = 'Application error';
                break;
        }
       
        // Log exception, if logger available
        if ($log = $this->getLog()) {
            $log->log($this->view->message, $priority, $errors->exception);
            $log->log('Request Parameters', $priority, $errors->request->getParams());
        }
       
        // conditionally display exceptions
        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }
       
        $this->view->request   = $errors->request;
    }

    public function getLog()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        if (!$bootstrap->hasResource('Log')) {
            return false;
        }
        $log = $bootstrap->getResource('Log');
        return $log;
    }


}


- error.phtml
Code: Select all
<!DOCTYPE html ....>
<html .......>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Zend Framework Default Application</title>
</head>
<body>
  <h1>An error occurred !</h1>
  <h2><?php echo $this->message ?></h2>

  <?php if (isset($this->exception)): ?>
 
  <h3>Exception information:</h3>
  <p>
      <b>Message:</b> <?php echo $this->exception->getMessage() ?>
  </p>

  <h3>Stack trace:</h3>
  <pre><?php echo $this->exception->getTraceAsString() ?>
  </pre>

  <h3>Request Parameters:</h3>
  <pre><?php echo $this->escape(var_export($this->request->getParams(), true)) ?>
  </pre>

  <?php endif ?>

</body>
</html>



I have done the above but I am still getting this error message

Code: Select all
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in /.../libzend-framework-php/Zend/Controller/Dispatcher/Standard.php:248 Stack trace: #0 /.../libzend-framework-php/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /.../libzend-framework-php/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch() #2 /.../libzend-framework-php/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() #3 /...../public/index.php(26): Zend_Application->run() #4 {main} Next exception 'Zend_Controller_Exception' with message 'Invalid controller specified (error)#0 /.../libzend-framework-php/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /..../libzend-framework-php/Zend/Controller/Plugin/Broker.php on line 336


Why does this only happen when I uncomment
Code: Select all
->setDestination(BASE_PATH."/../public/images")


Can someone please help.
davidtheo
 
Posts: 28
Joined: Fri Nov 11, 2011 11:41 pm

Re: Zend form file upload error

Postby us2rn4m2 on Tue Nov 20, 2012 3:30 pm

Why does this only happen when I uncomment ?
Code: Select all
->setDestination(BASE_PATH."/../public/images")

Because the destination doesn't exist or not correctly set !!

Did you resolve your issue ? NO ! So download zfFormModule.zip below, extract on your server and go to the url: http://localhost/zfFormModule/public/admin/main

There are inside everything you need !
zfFormModule.zip
(42.48 KiB) Downloaded 181 times


Hey Man !
- Your first: post Thu Oct 11
- My reply: Thu Oct 11
- Your second post: Thu Oct 25
You understand ?!
us2rn4m2
 
Posts: 49
Joined: Mon Jun 18, 2012 12:41 am


Return to Zend Framework

Who is online

Users browsing this forum: Google [Bot] and 6 guests