Overwrite uploaded file in Zend Framework - zend-framework

How do I upload a file, move it to a directory and keep the filename while overwriting any existing file? In Zend Framework.
This maintains the tmp filename (like php3382.tmp)
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->setDestination('data/');
$adapter->addFilter('Rename', array(
'target' => 'data/',
'overwrite' => true
));
$adapter->receive();
and this doesn't overwrite
$adapter = new Zend_File_Transfer_Adapter_Http();
$adapter->setDestination('data/');
$adapter->receive();

Try this:
...
$adapter->addFilter('Rename', array('overwrite'=> true,
'target'=> sprintf('data/%s',
$adapter->getFileName()),
));
$adapter->receive();
....

Related

Zend Framework Cache creates new file and doesnt load it

i'm having a little problem with Zend Framework Full Page Cache.
My current Bootstrap configuration looks like this:
$dir = PUBLIC_PATH . "/tmp/";
$frontendOptions = array(
'lifetime' => 6000000000,
'content_type_memorization' => true,
'default_options' => array(
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true,
),
'regexps' => array(
'^/.*' => array('cache' => true),
)
);
$backendOptions = array(
'cache_dir' => $dir
);
// getting a Zend_Cache_Frontend_Page object
$cache = Zend_Cache::factory('Page',
'File',
$frontendOptions,
$backendOptions);
$cache->start();
which worked perfectly before changing our development system to the live one.
Now if we enable the caching system it creates the correct cached file in the correct path but doens't load it.
So for every request another cache file is created but the old one NEVER gets loaded.
Maybe anyone has had this problems before and can give me a hint?
Thanks in advance!
there might be issue with permission to move from development environment to live.
The tmp directory is writable by myself and other users of the same group and also apparently Zend will access the files as another user. The solution was to chmod 777 on the folder, making it writable.
let me know if i can help you more.

How to move an image from temp folder to a folder inside the project in server in zend framework?

I have form where user can upload an image. I am working locally and when a user uploads a photo it added in the temp folder then I need to move it to folder inside the project. I need to know how to do this in live server.
Thanks
If you want to do it with Zend Framework, use Zend_Filter_File_Rename:
$moveTo = '/uploads';
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addValidator('Count', false, array('min' => 1, 'max' => 1))
->addValidator('Size', false, array('max' => '2000kB'))
->setDestination($moveTo);
if (!$upload->isValid()) {
return false;
}
$upload->receive();
$name = $upload->getFileName();
$newFile = "nefile.png";
$filterFileRename = new Zend_Filter_File_Rename(array(
'target' => $moveTo . '/' . $nefile;
'overwrite' => true
));
$file = $filterFileRename->filter($name);

Upload and encrypt a file using Zend_File_Transfer_Adapter_Http

In Zend Framework documentation titled "Filters for Zend_File_Transfer", it said file can be encrypted and saved as a different name using the filename option.
This filter supports one additional option which can be used to save the encrypted file with another filename. Set the filename option to change the filename where the encrypted file will be stored. If you suppress this option, the encrypted file will overwrite the original file.
How do I specify the filename option? I tried:
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addFilter('Encrypt', array('adapter' => 'mcrypt', 'key' => 'mykey'), 'file.txt');
and
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addFilter('Encrypt', array('adapter' => 'mcrypt', 'key' => 'mykey', 'filename' => 'file.txt'));
both don't seem to work.
I took a look into Zend/File/Transfer/Adapter/Http.php and it does not seem that filename is a valid option for this adapter.
Maybe this is a Bug in the Documentation.
Instead you could use the Rename-Filter to change the filename.
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->addFilter('Rename', array('target' => APPLICATION_PATH . '/../data/file.txt'));
$upload->addFilter('Decrypt', array('adapter' => 'mcrypt', 'key' => 'mykey'));
$upload->receive();

how to get files inside the folder to be zip Archive

$source="/Filelist/"
$filter = new Zend_Filter_Compress(
array(
'adapter' => 'Zip',
'options' => array(
'archive' => test.zip
)
)
);
$result = $filter->filter($source);
the function work fine ,the problem is that i want to get the file list inside the test.zip .but now i am getting the folder Filelist inside the archive.
it seems to because of line $content = str_replace(array('/', '\'), DIRECTORY_SEPARATOR, realpath($content)); and the realpath is removing '/'
Any solution ????????????????
Looking at the code, I don't see a way of doing that. Zend_Filter_Compress_Zip checks if it is a dir, and if it is, calls:
$zip->addEmptyDir(substr($local, 0, -1));

How to define the use of utf-8 in Doctrine 2 in Zend Framework application.ini, when using Bisna

The following ZendCasts cast, shows a way to use doctrine 2 in a zend framework environment.
Using this configuration, how can I make the connection use a utf-8 charset so the magic of "SET NAMES 'utf8'" will happen ?
What I'm really searching for is a way to configure it using the application.ini file.
If that's not possible using this configuration, how can this be done by code ? an _initDoctrine method in the Bootstratp file ?
Thank you.
UPDATE
It appears there's a post connect event which handles this, but I don't see how can I set it up via application.ini (if possible at all).
If not, can I set it up via a bootstrap method ? Will the bootstrap method run before any other doctrine connection code run, when relying on the Bisna library ?
If you are not using Bisna, you could simply do something like the following:
Pass the config stuff directly to EntityManager's connection options
(although driverOptions is not documented)
// $options is a simple array to hold your data
$connectionOptions = array(
'driver' => $options['conn']['driv'],
'user' => $options['conn']['user'],
'password' => $options['conn']['pass'],
'dbname' => $options['conn']['dbname'],
'host' => $options['conn']['host'],
'charset' => 'utf8',
'driverOptions' => array(
1002 => 'SET NAMES utf8'
)
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
I'm using the following custom bootstrap resource to initialize doctrine therefore $options is in application.ini and is accessible there by $this->getOptions();
// \library\My\Application\Resource\Doctrine.php
class My_Application_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract
{
public function init()
{
$options = $this->getOptions();
$config = new \Doctrine\ORM\Configuration();
//doctrine autoloader, config and other initializations
...
$connectionOptions = array(
.... //see above
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$registry = Zend_Registry::getInstance();
$registry->em = $em;
return $em;
}
}
It will bootstrap automatically if you put in application.ini
resources.doctrine.conn.host = '127.0.0.1'
resources.doctrine.conn.user = '...'
resources.doctrine.conn.pass = '...'
....
works fine for me
resources.doctrine.dbal.connections.default.parameters.driverOptions.1002 = "SET NAMES 'UTF8'"
1002 is the integer value of PDO::MYSQL_ATTR_INIT_COMMAND:
Command to execute when connecting to the MySQL server. Will
automatically be re-executed when reconnecting.
Note, this constant can only be used in the driver_options array when constructing a new
database handle.
this worked for me. config/autoload/doctrine.local.php
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => '...',
'password' => '...',
'dbname' => '...',
'driverOptions' => array(
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
)
),
)
)
)
);
It is possible to add it via application.ini, provided you use ZendX_Doctrine2 (at https://github.com/mridgway/ZendX_Doctrine2) with MySQL.
Then here's the line you need in application.ini:
resources.entitymanagerfactory.connectionOptions.driverOptions.1002 = "SET NAMES utf8"
(1002 == PDO::MYSQL_ATTR_INIT_COMMAND)
Don't forget to correctly set
default-character-set=utf8
in your my.cnf
Since this is for Doctrine 2, and ZendCasts is using Bisna, I believe you can just add this to your configuration.ini file
resources.doctrine.dbal.connections.default.parameters.driverOptions.charset = "utf8"
I'm not exactly sure how to test if it is sticking or not but let us know.
You could set the default table charset like that to utf8:
// Create new Doctrine Manager instance
$doctrineManager = Doctrine_Manager::getInstance();
// Set charset to UTF8
$doctrineManager->setAttribute(
Doctrine_Core::ATTR_DEFAULT_TABLE_CHARSET,
'utf8'
);
Quote:
an _initDoctrine method in the Bootstratp file ?
Yes.
For LoSo library and Doctrine 2 and MySQL add
resources.doctrine2.connection.driverOptions.1002 = "SET NAMES 'UTF8'"
to your application.ini
I have this in my bootstrap:
protected function _initDoctrineLibrary()
{
require_once('Doctrine/Doctrine.php');
$this->getApplication()->getAutoloader()->pushAutoloader(array('Doctrine', 'autoload'),'Doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(
Doctrine::ATTR_MODEL_LOADING,
Doctrine::MODEL_LOADING_CONSERVATIVE
);
$config = $this->getOption('doctrine');
$conn = Doctrine_Manager::connection($config['dsn'],'doctrine');
$conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
return $conn;
}
where in the application.ini you see
doctrine.dsn = "mysql://user:password#host/databasename"
I think you can do something similar