Upload and encrypt a file using Zend_File_Transfer_Adapter_Http - zend-framework

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

Related

Using the new 'data' option for CakeEmail attachments with CakePHP 2.4.x

The CakeEmail help page states that the data option has been added as of 2.4, so you no long have to have a physical file to add an attachment to an email.
I've got the following code:
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf', array('data' => $pdf)))
->send($this->request->data['email-message']);
But whenever I run that I get an Internal Error saying File Not Found: "".
I had a look at the source code (which I'm beginning to learn is often more useful than reading the documentation!): https://github.com/cakephp/cakephp/blob/master/lib/Cake/Network/Email/CakeEmail.php
Changing my code to:
$Email = new CakeEmail('default');
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf' => array('data' => $pdf, 'mimetype' => 'application/pdf')))
->send($this->request->data['email-message']);
Notice on the attachments line, the array is assigned to the filename variable, rather than passed in as a parameter!
For completeness, if anyone else is reading this and is wondering how I am generating my PDF with CakePDF:
// Create PDF for attachment
$CakePdf = new CakePdf();
$CakePdf->template('claim', 'default');
//get the pdf string returned
$pdf = $CakePdf->output();

How to update an image file

I am trying to update an image of a database that contains data about students. I have used filefield and i want that firstly to display the name of the image that already exists in databse and than to change it if i want.
$cInput_form .= $q->div({-class => 'control-group'},
$q->div({-class => 'controls'},
$q->div({-class => 'span12'},
$q->filefield({-name => 'myfile', -value => $image, -label => $image }))));
where $image is the name of the picture that i get from database. But the problem is that this name doesn't appears. It is 'No file choosen'. label doesn't work, what may be the problem?
IFAIK, file field cannot be pre-filled for security reason.

Overwrite uploaded file in 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();
....

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

Image Upload with Zend_Service_Nirvanix

I can't seem to upload an image using Zend_Service_Nirvanix. Is it even possible?
I have a feeling that my problem has something to do with not being able to figure out how to set the UploadHost on the Transfer Service.
Any help is greatly appreciated! My deadline is July 16th!
Here is my code:
$nirvanix = new Zend_Service_Nirvanix(array('appKey' => $key,
'username' => $user,
'password' => pass));
$NSImfs = $nirvanix->getService('IMFS');
$options = array('sizeBytes' => filesize($source));
$storageNode = $NSImfs->getStorageNode($options);
$NSTransfer = $nirvanix->getService('Transfer');
$options = array('uploadToken' => $storageNode->getStorageNode->UploadToken,
'path' => $original,
'fileData' => file_get_contents($source));
$result = $NSTransfer->uploadFile($options);
Here is the error I keep getting:
Zend_Service_Nirvanix_Exception: XML
could not be parsed from response:
Server Error in '/' Application. The
resource cannot be found. Description:
HTTP 404. The resource you are looking
for (or one of its dependencies) could
have been removed, had its name
changed, or is temporarily
unavailable. Please review the
following URL and make sure that it is
spelled correctly.
Requested URL:
/ws/Transfer/UploadFile.ashx
in
/Applications/MAMP/bin/php5/lib/php/Zend/Service/Nirvanix/Response.php
on line 119
You're getting a 404?
Have you checked for an updated version of that library?
Try going into the libray and changing UploadFile.ashx to UploadFile.aspx. I don't think ashx is not a standard extension.
Maybe that will fix it.
There's a commercial upload tool from Aurigma that has support for file and image upload to Nirvanix. Here's the link (see Uploading to Nirvanix section there) to the help topic to check.
To do a local upload (rather than a web upload via the browser) you just have to call the putContents method passing the files data.
Example:
$nirvanix = new Zend_Service_Nirvanix(array('appKey' => $key,
'username' => $user,
'password' => pass));
$NSImfs = $nirvanix->getService('IMFS');
$response = $NSImfs->putContents($destination_file_and_path,
file_get_contents($source_file));
if($response->ResponseCode != 0)
{
echo 'Fail!';
}
You would only call GetStorageNode if you want to generate a token to pass a browser the upload token.