简体   繁体   中英

allow php to write log files

I have a php script I want to create log files with. But it wont do it, permission denied. syntactically, i think my code is correct for writing files, but I don't know how to (safely) allow php to make files in a directory.

> [Wed May 07 07:01:28.827861 2014] [:error] [pid 2457] [client
> 10.0.0.123:63383] PHP Warning:  fopen(Log filename: 1399460488.txt): failed to open stream: Permission denied in /home/j0h/www/test/iv.php on line 7, referer:
> http://10.0.0.123/test/

this is a linux server using php5.4 here is the code I am trying to use to write the file: Line 7 starts with $handle ...

<?php
//Time... for some logs
$t=time();
//create Log filename based of Epoch
$LogName =  $t . ".txt";
$handle = fopen($LogName, 'w') or die('Cannot open file:  '.$LogName); 

$file = fopen( $LogName, "w" );
if( $file == false )
{
   echo ( "Error in opening new file" );
   exit();
}
fwrite( $file, "This is  a simple test\n" );
fclose( $file );
?>

Quick answer : add the write permission to the directory you are writing log to :

chmod -R +w /home/j0h/www/test/

However, for a more secure way, you should write your log to a specific log directory, without any php or executable file in it.

It's probably not an php issue. It's the filesystem that prevents writing access. There are several solutions: Give the directory 777 rights, let your webuser own the directory or let it join a group that has permissions, ...

Navigate to the log directory and try sudo chmod -R 777 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM