2012-10-02 71 views
0

文本文件在我的PHP应用程序,我想创建一个错误日志中这样尝试过这样的工作的罚款在我的本地机器的文本格式PHP创建服务器

if(!$mail->Send()) 
{ 
echo "Message could not be sent. <p>"; 
echo "Mailer Error: " . $mail->ErrorInfo; 
$stringData = "Error Info: ".$mail->ErrorInfo."\t\t"; 
$stringData .= "Email to reciepient \t Regnumber: ".$RegNo." \t Apllicant Name: ".$ApplicantName." Failed --- end ----"; 


$fp = fopen($_SERVER['DOCUMENT_ROOT']."/lib/email_errorlog.txt","wb"); 
fwrite($fp,$stringData); 
fclose($fp); 

exit; 
    } 

我已经在PHP Create and Save a txt file to root directory看到讨论,但它不适合我。问题是,没有显示错误,但文本文件没有创建。需要在服务器上设置任何权限?

回答

2

你必须确保:

  • 文件夹/ lib目录中的文档根目录
  • webserver process有权写入该文件夹存在。

如果您创建了您的FTP帐户的文件夹,Web服务器进程会有没有访问。您可以将权限设置为777,但每个人都可以访问。最好的办法是将权限设置为770,并将该文件夹的组作为Web服务器组标识。

+0

是的,问题是与文件夹权限,现在我纠正了,并正常工作..谢谢。 – Rakesh

0

在尝试创建文件之前,您可能会检查文件(或者父目录)is writeable

而且根据 fopen()

成功返回文件指针资源,或错误FALSE。

所以,你可以使用此+ $php_errormsgget_last_error()打造正确文件编写代码:

$fp = fopen($_SERVER['DOCUMENT_ROOT']."/lib/email_errorlog.txt","wb"); 
if($fp === false){ 
    // Notification about failed opening 
    echo "Cannot open file: " + $php_errormsg; // Not that wise in production 
    exit(); 
} 

fwrite($fp,$stringData); 
fclose($fp); 
exit(); 

但随着配置正确所有错误应该在错误日志。