dsqbkh3630
dsqbkh3630
2012-07-04 08:59

使用php创建txt文件失败

已采纳

I am trying to create a file and write a message in a folder called debug:

 function debug($report){
   $file="/debug/debug.txt";
   file_put_contents($file,$report,FILE_APPEND | LOCK_EX);
 }

The debug folder is in my root directory. But file fails to be created.. Why?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • duanqi6274 duanqi6274 9年前

    This code will open all the permissions for the folder.

    chmod('PATH_TO_FOLDER',0777);
    

    Depending on what exactly you want to do with that folder, you might consider giving it slightly more limited permissions that allow only specific users to read/write to it.

    Here are some other examples taken from the PHP manual - http://php.net/manual/en/function.chmod.php

    <?php
    // Read and write for owner, nothing for everybody else
    chmod("/somedir/somefile", 0600);
    
    // Read and write for owner, read for everybody else
    chmod("/somedir/somefile", 0644);
    
    // Everything for owner, read and execute for others
    chmod("/somedir/somefile", 0755);
    
    // Everything for owner, read and execute for owner's group
    chmod("/somedir/somefile", 0750);
    ?>
    
    点赞 评论 复制链接分享
  • doulaozhang0238 doulaozhang0238 9年前

    This is really a codicil to Lix's answer.

    The most frequent reason for this is when you are running you scripts under Apache using mpd_php5, since your php process will be running as apache UID (www-data or whatever). However, by default your debug directory will be own by your account with 755 permissions.

    I would suggest that it is bad practice to set the permissions to 777 as this will allow anyone to create files in your directory, but instead temporarily set the permissions to 777 then use a Q&D script to create a debug/logs and set its permissions to 755, before returning the debug directory to 755.

    You will find that the debug/logs directory is now own by the Apache UID, and can be written to by any Apache process.

    One wrinkle here is that you will need to use a script to delete old debug files as your own account won't have the privilege to do this.

    点赞 评论 复制链接分享
  • dongqigu0429 dongqigu0429 9年前

    Nothing wrong with that code, if errors not shown maybe caused be php configuration. Please see at Error Reporting Error Report.

    Generally it may caused by permission ( see permission on debug folder )

    点赞 评论 复制链接分享