2013-03-28 58 views
1

你有一个奇怪的问题,在cakephp中的File :: write()。cakephp - 保存文件syncronized并作为附件发送

我写这段代码来生成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('[email protected]' => '......')); 
    $Email->to('[email protected]'); 
    $Email->subject('Nuovo ordine'); 

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

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

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

} 

我第一次运行该代码的电子邮件不工作,电子邮件的工作不错,如果只PDF文件在文件夹中。

我认为,文件::写执行某种异步写的,当系统运行电子邮件附件::文件ins't准备。

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

非常感谢。

回答

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

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

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

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

相关问题