CakePHP Email Sending 0 bytes file attachment - cakephp

I need to attach a recent file to an email. The attachment is there, but it is 0 bytes. The path is correct
I have the following code, in part:
$file = WWW_ROOT . $handle->file_dst_pathname;
/* $file = /var/www/app/webroot/files/uploads/children/0000000220.pdf which is correct */
$attachment_name = 'Progress Note for ' . $parent['Child']['child_name'] . '-' . date('mdY') . '.' . $data['extension'];
$Email->attachments(array($attachment_name => $file));
I am using Cakephp 2.4.9
Any help is appreciated.
Greg

according to the docs
$Email->attachments(array(
$attachment_name => array(
'file' => '$file'
)
));

It probably depends on the version of CakePHP you are using (2.x) and probably a version of PHP. I was struggling with the 0 bytes attachments as well. I had to dig in the vendor email file. Basically it turned out, that the _readFile() fn was messing things up with CakePHP File Reader.
So what worked for me was to specify the filename, specify the full path and attach the data of the file in base64. Then everything worked. All other options from documentation didnt work. I was running PHP 5.4.45 (old one).
'filename' = array(
'file' => '/full/path/to/the/file/on/disc.pdf',
'data' => chunk_split(base64_encode(file_get_contents('/full/path/to/the/file/on/disc.pdf')))
);

Related

Using CakePdf data not showing

I am in a problem with CakePDF. I am using CakePHP 2.3.6. I setup everything alright, bootstrap & core files are ok.
Now, I want to generate many pdf files and download them. Downloading is ok, but when I download the files, I see that the generated pdf files say that Undefined $data, where I send $data to the view file.
Here is my Controller code:
App::uses('CakePdf', 'CakePdf.pdf');
// ...
public function createPdf($id) {
$data = $this->DBTable->find('all', array(
'conditions' => array(
'DBTable.id' => $id
)
));
$CakePdf = new CakePdf();
$path = APP . DS . 'pdf' . DS;
foreach($data as $key => $var) {
$CakePdf->template('create', 'default');
$this->set('var', $var);
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => $var['id'] . ".pdf"
);
$CakePdf->write($path . $var['id'] . ".pdf");
}
}
Here, I am downloading the files some other way, not related to this problem, because downloading is ok. When I run this, all files are downloaded, and when I see the files, they say "Udefined var", looks like they didn't receive and variable called "$var".
I put the view file & the layout exactly where ceeram said.
What should I do now ? Please help me.
At last I got a solution to this. In the Controller function :
$data=$this->ModelName->find('all');
$CakePdf=new CakePdf();
$CakePdf->template('view_file','default');
$this->pdfConfig=array('orientation'=>'portrait','filename'=>$data['name']);
$CakePdf->viewVars(array('data'=>$data));
$CakePdf->write($path.$data['name'].".pdf");
Here, we have to store view_file.ctp in View/Pdf/, and default in View/Layouts/pdf/, where view_file.ctp is the view file which we want to convert in pdf, and default is the layout for this specific view file.
Here, viewVars() function passes the data to the view file, just like :
$this->set->('data',$data);
Let me know if anything is not clear here.
Thanks.

Cake PHP file Downloading using Media view

Hi I uploaded files and stored in app/views/xxx/yyy.
Now i would like to download the files
If the file is PDF it is opened in new Tab and rest of them are downloaded, I need all files to be opened in new tab.
Here is my Code :
$this->view = 'Media';
$params = array(
'id' => $fileName,
'name' => $fileNameWOExtn,
'download' => false,
'extension' => $fileExtn, // must be lower case
'path' => APP . 'views' . DS .'static' . DS. $url .DS // don't forget terminal 'DS'
);
LogUtil::$logger->debug('Media View Params : '. var_export($params, true));
$this->set($params);
Not every file can be opened in the browser. A lot of file type encodings are proprietary and require their specific program to read and interpret them. PDFs, plain text files, some images, etc (with some exceptions) are the only types that browsers can recognize.
What types are files are you trying to render?

Wrong File Type for File Download - CakePHP

In my cakephp web page, I have a button that the user clicks and it performs some maintenance on the server and then presents a download to the user. The file download is testlogs.gzip but when the download option occurs in firefox, it comes up as a file type of html document.
The file downloads and extracts fine, but I want the file type to be correct.
Here is the Media view class for the download:
//$this->autoRender = false;
$this->viewClass = 'Media';
$params = array(
'id' => 'systemlogs',
'name' => 'testsystemlogs',
'download' => true,
'extension' => 'gzip',
// 'mimeType' => 'zip',
'path' => DS . 'home' . DS
);
$this->set($params);
I tried to add in the 'mimeType' => 'zip' but that did not work.
Any help would be great.
Thanks
UPDATE: I tested uploading of the same files and I used firebug to determine the content type. The only file types I care about are of type: application/octet-stream.
So I think I just need to set this type in the Media class settings, not sure how to do that though.
THanks
mimeType has to be specified as an associative array (extension -> type), in your case this would be array('gzip' => 'application/x-gzip'). CakePHP merges this array with the built-in array of known MIME types in CakeResponse class.
However, CakePHP already knows the MIME type of .gz files so changing the extension of your file, if it is an option, might be an even easier solution.

How to use MeioUpload Behavior for uploading any kind of files not only images

I would like to use the MeioUpload Behavior for uploading any kind of documents(I want every extension to be accepted). I've already seen this question , but it didn't work for me, for some strange reason I can only upload image and pdf files for the other types of files I get this error when I attempt to submit the form : "The post could not be saved. Please, try again."
Edit: Well, it looks like I was finally able to upload other kind of files apart from images,I had to write the options 'allowedMime' and 'allowedExt' in camelCase (in the documentation they use the underscore version 'allowed_mime', 'allowed_ext' I don't know why:( ), but I haven't been able to upload .zip files and most importantly I still don't know how to tell the behaviour to accept anything
var $actsAs = array(
'MeioUpload' => array(
'link_referencia' => array(
'dir' => 'files{DS}uploads',
'create_directory' => true,
'allowedMime' => array('application/pdf', 'application/msword', 'application/vnd.ms-powerpoint', 'application/vnd.ms-excel', 'application/rtf', 'application/zip'),
'allowedExt' => array('.pdf', '.doc', '.ppt', '.xls', '.rtf', '.zip'),
'default' => false,
)
)
);
Thanks in advance
It can be use for anything, hence it's called meioUpload and not meioImage
Just set the allowed file extensions and mime types when you initialiave the behavior.
Step 4 has an example http://www.meiocodigo.com/projects/meioupload/

CakePHP Media Plugin version 1.3, UUID filenames

Is anybody else using David Persson's media plugin for CakePHP? I'm struggling with setting up some features of the latest version. I'd like to set it up to make a UUID-based filename for uploaded images, but I'm not sure how to go about it.
I will fight with it some more, but I'm posting to find out if anybody here can tell me if the 1.3 is generally working or generally NOT working.
Finally got this (partially) working. The UUID filename stuff works when I place the following code in my Attachment model:
function transferTo($via, $from) {
extract($from);
$irregular = array(
'image' => 'img',
'text' => 'txt'
);
$name = Mime_Type::guessName($mimeType ? $mimeType : $file);
if (isset($irregular[$name])) {
$short = $irregular[$name];
} else {
$short = substr($name, 0, 3);
}
$path = $short . DS;
$path .= String::uuid();
$path .= !empty($extension) ? '.' . strtolower($extension) : null;
return $path;
}
I'm still having some trouble with other parts of the Media Helper, but the author posted some updates to his git repository today (Jul 17 2010).

Resources