ryan
ryan

Reputation: 333

path to be given in setDestination method of Zend_Form_Element_File in Zend Framework

I am trying to upload a file in Zend framework. I have following code in my form:

 $this->addElementPrefixPath('App', 'App/');
            $this->setName('upload');
    $this->setAttrib('enctype', 'multipart/form-data');

    $description = new Zend_Form_Element_Text('description');
    $description->setLabel('Description')
            ->setRequired(true)
            ->addValidator('NotEmpty');
    $file = new Zend_Form_Element_File('file');
    $path="/images";
    $file->setDestination($path)
            ->setLabel('File')
            ->setRequired(true)
            ->addValidator('NotEmpty');  

    $submit = new Zend_Form_Element_Submit('submit');

    $submit->setLabel('Upload');

    $this->addElements(array($description, $file, $submit));

I am using netbeans and I have one folder named images under public folder. I want that all files are uploaded into images folder but when I run the project it gives the error as

The given destination is not a directory or does not exist

If I give the full path as C:\Users\398853\Documents\NetBeansProjects\ZendWithFileUpload\public\images then it runs fine and the file gets uploaded into images folder. But when I give $path='/images' It says above error'Why? What should be the path given?Thanks.

Upvotes: 0

Views: 7310

Answers (3)

Ilyas Fasikhov
Ilyas Fasikhov

Reputation: 7

add config to module.config.php

return array (
    ....
    //other settings
    'module_config' => array(
        'upload_location'   => __DIR__.'/../../../data/uploads'
    )   
);

add controller

public function getFileUploadLocation() {
    $config = $this->getServiceLocator()->get('config');
    return $config['module_config']['upload_location'];
}

and use ....

$uploadPath = $this->getFileUploadLocation();
$adapter->setDestination($uploadPath);

....

Upvotes: 1

turist_ua
turist_ua

Reputation: 114

In SetDestination should be setted absolute path to desitnation folder (/var/www/... or c:/webserver/... in case when you win-user).

But good practice is to get absolute path with functions getcwd() or realpath(dirname(FILE)).

So, at first you shoud define constant (for example with name PUBLIC_PATH) in index.php file (usually situated in public-folder).

defined('PUBLIC_PATH')
|| define('PUBLIC_PATH', realpath(dirname(__FILE__)));

This way you can use variable PUBLIC_PATH instead of writting string "/var/www/..." as prefix to your real path to upload folder.

So, if your "images" folder situated in "public" folder, you should user such construction:

$file->setDestination(PUBLIC_PATH . '/images');

This code also will work after upload project from localhost to webserver.

Upvotes: 4

Raj
Raj

Reputation: 22926

In your bootstrap code, you need to set your application's root folder.

Whenever you want to get the path of a subfolder in your application folder (eg. images in your scenario), you need to use this variable and append your folder name.

If your bootstrap code is in the Application folder which is under the public root folder,

Zend_Registry::set('APP_ROOT', dirname(dirname(__FILE__)));

In other places of your application, when you want get a folder path, like you want to get the image folder path under the public folder, u need to use,

Zend_Registry::get('APP_ROOT') . "/images";

Upvotes: 0

Related Questions