$ch = curl_init(); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIEJAR, 'patch_to_file'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'patch_to_file'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, 'data'); curl_setopt($ch, CURLOPT_URL, 'url'); curl_exec($ch); var_export(unlink('patch_to_file')); //true, но файл все равно присутствует по указанному пути 

Why is the file in which cookies are written not deleted?

I need to tear down the cookie for further work without closing the curl session.

  • There is one observation: if you close the curl session - curl_close ($ ch), then the file is deleted as it should, but I need to continue to work on the code with this identifier of the curl - user193361
  • then delete at the very end, without curl_close, the file is locked by the curl process, you cannot delete it, well, maybe it can be, depending on the OS, but should not do so. - strangeqargo
  • so I need to take down the cookie for further work - user193361
  • well, curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ("Cookie: test = cookie")); what is not happy? you put it in your meaning. And in general, you have one question (why the file is not deleted), but in fact is completely different (how to take down the cookie in the hen) - strangeqargo 7:49 pm
  • those. you just don’t pass the desired cookie or pass an empty value (if you need it) to the php manual: Note: multiple cookies are followed by a space (eg, "fruit = apple; color = red" - strangeqargo

1 answer 1

1) the file is not deleted because it is used by the curl, because you did not call (as you found it) curl_close . At least in Linux, unlink will return true "delete ok" type to you, but as long as the file is used by live processes, it will not be deleted. But after exiting the script, it will most likely disappear completely. If you don’t pick it up by any other process until the script is released.

2) if you need to override or delete cookies, use

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: test=cookie")); or CURLOPT_COOKIE you can make the header empty (do not send cookies), transfer the necessary cookies (via ; ), redefine the necessary ones, etc.