cakephp - 保存文件同步并作为附件发送

时间:2013-03-28 08:42:10

标签: cakephp

你在cakephp中的File :: write()有一个奇怪的问题。

我编写此代码以生成pdf视图并将其保存在文件中。

private function generate( $id ){

    $order = $this->Order->read( null, $id );
    $this->set('order', $order);

    $view = new View($this);

    $viewdata = $view->render('pdf');

    //set the file name to save the View's output
    $path = WWW_ROOT . 'ordini/' . $id . '.pdf';
    $file = new File($path, true);

    //write the content to the file
    $file->write( $viewdata );

    //return the path
    return $path;

}

我需要生成这个pdf并将其作为attacchment发送,这是我的代码

public function publish ($id) {
    $pdf_file = $this->generate( (int)$id );

    $Email = new CakeEmail();
    $Email->from(array('info@.....com' => '......'));
    $Email->to('....@gmail.com');
    $Email->subject('Nuovo ordine');

    $filepath = WWW_ROOT . 'ordini/' . $id . '.pdf';

    $Email->attachments(array('Ordine.pdf' => $filepath));

    $Email->send('......');

}

我第一次运行此代码时,电子邮件不起作用,只有当pdf在文件夹中有效时,电子邮件才能正常工作。

我认为File :: write执行某种异步写入,当系统执行Email :: attachments时,文件还没准备好。

如何在此代码中插入while()来检查文件的可用性?

非常感谢。

2 个答案:

答案 0 :(得分:1)

if($file->write( $viewdata ))
{
   return $path;
}

答案 1 :(得分:0)

使用返回值是不够的。 我改变了这一行

$file = new File($path, false);

打开文件而不强迫我工作。