好吧我设法解决了我的问题,但现在的问题是,我不能删除ftp中的文件夹和文件,它应该设置为777但我只是无法摆脱文件夹& txt文件,以后需要该选项来偶尔清理日志..
另外,有没有办法清理代码升级? 你看到更好的方法吗?谢谢!
// create log for security messures, because user returned zero on checkup
$logusername = $_POST['username'];
$userAgent = ( isset($_SERVER['HTTP_USER_AGENT'])
&& ($_SERVER['HTTP_USER_AGENT'] != ""))
? $_SERVER['HTTP_USER_AGENT'] : "Unknown";
$userIp = ( isset($_SERVER['REMOTE_ADDR'])
&& ($_SERVER['REMOTE_ADDR'] != ""))
? $_SERVER['REMOTE_ADDR'] : "Unknown";
$refferer = ( isset($_SERVER['HTTP_REFERER'])
&& ($_SERVER['HTTP_REFERER'] != ""))
? $_SERVER['HTTP_REFERER'] : "Unknown";
$uri = ( isset($_SERVER['REQUEST_URI'])
&& ($_SERVER['REQUEST_URI'] != ""))
? $_SERVER['REQUEST_URI'] : "Unknown";
$hostname = gethostbyaddr($userIp);
//write log to file
$log_path = date("d_m_y");
$log_file = date("d_m_y");
$full_file_path = "falsehistory/$log_path/$log_file.txt";
// check if backup have user folder
if (!file_exists("$full_file_path"))
{
$f = @fopen($full_file_path, 'w');
// creating a new directory for the backup user
mkdir("falsehistory/$log_path/", 0777);
$f = @fopen($full_file_path, 'a+');
@fputs($f, "".date("d.m.Y g:ia").": \n ".$logusername." \n ".$userIp." \n ".$hostname." \n ".$refferer." -> ".$uri." \n - \n");
@fclose($f);
}
else
{
$f = @fopen($full_file_path, 'a+');
@fputs($f, "".date("d.m.Y g:ia").": \n ".$logusername." \n ".$userIp." \n ".$hostname." \n ".$refferer." -> ".$uri." \n - \n");
@fclose($f);
}
答案 0 :(得分:1)
您可能希望尝试使用mkdir()
并将递归参数设置为true
:
mkdir($full_file_path, 0777, true);
它将确保创建所有深度级别的文件夹。
此外,您的代码有很多内容可以悄悄地要求改进:
1。)您不需要将变量名称放在引号中:
例如:
mkdir("$full_file_path", 0777);
能够而且应该是
mkdir($full_file_path, 0777);
2。)如果你将固定字符串与变量组件分开,它在光学上和句法上都有帮助。
例如:
$full_file_path = "falsehistory/$log_path/$log_file.txt";
可能成为:
$full_file_path = "falsehistory/" . $log_path . "/$log_file.txt";
3。)您在将数据附加到文件时遇到了一些麻烦。
$f = @fopen($full_file_path, 'a+');
@fputs($f, "".date("d.m.Y g:ia").": \n ".$logusername." \n ".$userIp." \n ".$hostname." \n ".$refferer." -> ".$uri." \n - \n");
@fclose($f);
使用file_put_contents()
(see the PHP manual here)
$s=date("d.m.Y g:ia") . ": \n ".$logusername." \n ".$userIp." \n ".$hostname." \n ".$refferer." -> ".$uri." \n - \n";
file_put_contents($full_file_path, $s, FILE_APPEND);