0

the fopen() fails and it outputs

Warning: fopen(logs/response_rowbody/230.mlog): failed to open stream: No such file or directory in C:\xampp\htdocs\test.php on line 5

after hours of isolating the reasons that might be causing the problem, I found it is because the function is called in function registered with register_shutdown_function . and I don't know if this is normal or not!

this is the code that I use

this code works

the file C:\xampp\htdocs\test.php

<?php
//register_shutdown_function('logresponse'); 
/*the function logresponse() works here because it has been called naturaly at the end of the file without being shut_down_registered*/
logresponse();
function logresponse(){
   $response_rowbody = "logs/response_rowbody/230.mlog";
   $fhandle = fopen($response_rowbody,"wb");if(!$fhandle){echo"errorrr";};
exit;};
?>

this code does not work

the file C:\xampp\htdocs\test.php

<?php
register_shutdown_function('logresponse'); 
/*the function logresponse() does NOT work here because it has been called as a shut-down registered function*/
//logresponse();
function logresponse(){
   $response_rowbody = "logs/response_rowbody/230.mlog";
   $fhandle = fopen($response_rowbody,"wb");if(!$fhandle){echo"errorrr";};
exit;};
?>

what am I doing wrong here?? thanks.

Accountant م
  • 6,975
  • 3
  • 41
  • 61

2 Answers2

1

From the manual:

Working directory of the script can change inside the shutdown function under some web servers, e.g. Apache.

That means you cannot trust relative paths. Use absolute paths instead.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
-1

Maybe you have to adjust the path to a static path. Like c:\\folder\\resource.txt

Patrick Mlr
  • 2,955
  • 2
  • 16
  • 25