file_put_contents() failed to open stream: Permission denied 问题解决(生成日志文件)

时间:2023-01-16 21:28:39
很长时间没有写PHP了,今天突然有个需求要写一个保存文件的功能。

function downloadFile( $url , $savePath = '' )
{
$fileName = getUrlFileExt( $url );
$fileName = rand(0,1000). '.' . $fileName ;
$file = file_get_contents ( $url );
file_put_contents ( $savePath . '/' . $fileName , $file );
return $fileName ;
}



调用downloadFile( "http://www.xxx.com" , "/bak" );

怎么都不行,一直提示file_put_contents() failed to open stream: Permission denied

后面把文件夹权限也加上还是不行,郁闷了。

最后突然想到地址不应该是相对的哦。

改为downloadFile( "http://www.xxx.com" , rtrim($_SERVER['DOCUMENT_ROOT'],'/')."/bak" ); 搞定了,哎要是换成以前肯定一下就想到了。


《《《《《《《《《《===============================================================================================》》》》》》》》

<pre name="code" class="php">public function setDefaults(){
$success= "更新成功";
$faild = "更新失败";
$sql ="UPDATE config SET withdraw = 1000,max_money = 1000"; //SQL语句
$this->db->query($sql);
if(mysql_error()){
$this->writelogs($faild);
return false;
}
$this->writelogs($success);
echo 'OK';
return true;
}

//记录日志 txt
public function writelogs($data) {
$base_path = rtrim($_SERVER['DOCUMENT_ROOT'],'/')."/logs/";
$myfile = $base_path . "time_log.txt";
$times = date('Y-m-d H:i:s', time());
//$string = $times ."=========>" $data."\n";
$string = $times.'======>'.$data."\n\r";
file_put_contents($myfile, $string , FILE_APPEND);
}