PHP create text file on server

前端 未结 2 1905
囚心锁ツ
囚心锁ツ 2021-01-29 03:52

In my php application i want to create a error log in a text format so tried like this its working fine in my local machine

if(!$mail->Send())
{
 echo \"Mess         


        
相关标签:
2条回答
  • 2021-01-29 04:27

    You have to make sure that:

    • the folder /lib exists in the document root
    • the webserver process has permission to write to that folder.

    If you create the folder with your ftp account, the webserver process will have no access. You can set permissions to 777, but then everyone has access. Best would be to set permission to 770 and make the group of the folder the webserver group id.

    0 讨论(0)
  • 2021-01-29 04:39

    You may check whether file (or rather parent directory) is writeable before trying to create file.

    And according to php manual fopen():

    Returns a file pointer resource on success, or FALSE on error.

    So you could use this + $php_errormsg or get_last_error() to build correct file writing code:

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

    But with correct configuration all errors should be in error log.

    0 讨论(0)
提交回复
热议问题