php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49517 cURL's CURLOPT_FILE prevents file from being deleted after fclose
Submitted: 2009-09-10 06:16 UTC Modified: 2009-09-30 02:34 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: niraj at bhawnani dot name Assigned:
Status: Closed Package: cURL related
PHP Version: 5.*, 6 (2009-09-09) OS: *
Private report: No CVE-ID: None
 [2009-09-10 06:16 UTC] niraj at bhawnani dot name
Description:
------------
I spent hours debugging some code I'd written that wasn't working only to find that cURL is behaving strangely. I've taken the minimal parts of that code and put it in this bug report.

Basically, if you open a file with fopen, fclose it and then unlink it, it works fine. But if between fopen and fclose, you give the file handle to cURL to do some writing into the file, then the unlink fails. Why this is happening is beyond me. I think it may be related to Bug #48676

I have not had the opportunity to test this on Linux, so I don't know if it's Windows-only.

Reproduce code:
---------------
<?php
$filename = 'tempfile';
$fp = fopen($filename, 'w');
if (!$fp)
	exit("Failed to open file for writing");
$curl = curl_init('http://bugs.php.net/gifs/logo-bug.gif');
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); // 10 sec
curl_setopt($curl, CURLOPT_TIMEOUT, 600); // 10 mins
curl_exec($curl);
curl_close($curl);
fclose($fp);
unlink($filename);
?>

Expected result:
----------------
No errors, file unlinks successfully.

Actual result:
--------------
Warning: unlink(tempfile) [function.unlink]: Permission denied in C:\...\blah.php on line 14


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-10 06:27 UTC] niraj at bhawnani dot name
It is definitely related to Bug #48676, if I fclose($fp) twice, it works.

Here's the same code with a workaround, that makes the unlink work fine:

<?php
$filename = 'tempfile';
$fp = fopen($filename, 'w');
if (!$fp)
	exit("Failed to open file for writing");
$curl = curl_init('http://bugs.php.net/gifs/logo-bug.gif');
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); // 10 sec
curl_setopt($curl, CURLOPT_TIMEOUT, 600); // 10 mins
curl_exec($curl);
curl_close($curl);
fclose($fp);
if (is_resource($fp))
	fclose($fp);
unlink($filename);
?>
 [2009-09-10 08:00 UTC] sjoerd@php.net
Could reproduce.

<?php
$filename = tempnam('/tmp', 'bug49517');
$fp = fopen($filename, 'w');
$curl = curl_init();
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_close($curl);
fclose($fp);
var_dump(is_resource($fp)); // expected: false, because $fp is closed.
?>
 [2009-09-30 02:34 UTC] svn@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&revision=288973
Log: Fixed bug #49517 (cURL's CURLOPT_FILE prevents file from being deleted after fclose).
 [2009-09-30 02:34 UTC] iliaa@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2009-10-10 09:25 UTC] svn@php.net
Automatic comment from SVN on behalf of pajoye
Revision: http://svn.php.net/viewvc/?view=revision&revision=289495
Log: - Merge: Fixed bug #49517 (cURL's CURLOPT_FILE prevents file from being deleted after fclose)
 [2009-10-10 09:29 UTC] svn@php.net
Automatic comment from SVN on behalf of pajoye
Revision: http://svn.php.net/viewvc/?view=revision&revision=289496
Log: - Merge: Fixed bug #49517 (cURL's CURLOPT_FILE prevents file from being deleted after fclose)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 05:01:30 2024 UTC