The following script (stripped down for clarity and simplicity) has run fine for years, but since upgrading from php 5 to 8, the cookie file created is no longer deleted by the unlink command, despite it reporting success, leading to a build-up of these cookie files.
If I take out the CURLOPT_COOKIEJAR line, then the file is deleted just fine.
<?php
$my_cookie=tempnam("./","XX");
$ch = curl_init();
curl_setopt ($ch, CURLOPT_COOKIEJAR, $my_cookie);
curl_close($ch);
if (unlink($my_cookie)) {
echo "success - $my_cookie unlinked<BR>";
} else {
echo "fail";
}
?>
Any ideas?
2
Answers
Try using an absolute path: curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath($my_cookie) );
curl_close($ch);
does nothing in PHP >= 8.0.0, so the cookie file doesn’t actually exist (yet) when you’re trying to delete it.Replace
curl_close($ch);
withunset($ch);
. This will write the cookie file.See documentation of
curl_close
: