Title: php Curl download tbz file?
Tags: php;curl;download
Question: I checked SO and also googled but not find any way to resolve my issue.
i want to download a file with tbz extension but it is giving me error as
Warning: feof() expects parameter 1 to be resource, string given in D:\xampp\htdocs\test\testing.php on line **
I am using curl to download this file from the server.
set_time_limit(0);
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $webpage_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$handler = curl_exec($ch);
$fp = fopen("adminArea/test/$file", "w+");
$contents = '';
while (!feof($handler)) {
$contents .= fread($handler, 8192);
fwrite($fp, $handler);
}
fclose($fp);
curl_close($ch);
my files are in MB and when i execute this i get file of a larger size. if my file is 16m of size when i run the code it takes alot time and when i stopped it manually it was 146MB of size. what i am doing wrong?

Here is the accepted answer: I don't believe the type of file you're trying to access has much impact on how the code works.
Firstly, when CURLOPT_RETURNTRANSFER is set, curl_exec will return the data.
Secondly, feof, fread etc require a file handle, not a cURL handle or the returned data (which is why your error message is showing that you're sending a string through).
Your code should probably be something like this instead:
set_time_limit(0);
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $webpage_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
file_put_contents("adminArea/test/$file", curl_exec($ch));
curl_close($ch);
Although I would personally test for errors before committing anything (unless this is, say, just for personal once-off use).
Comment for this answer: thanks for the reply. I tried the same kinda code before but this made a file of 1KB only on my directory. So i thought to do it file handling way. This code only creates a file of 1KB for me not of the actual size.
Comment for this answer: I doubt file handling would mess up your file - more likely, the external server is sending a 1KB response rather than what you had been expecting.  Many possible reasons for this, for example, this redirects the user to the actual file, or the site requires a POST request, or the site requires a certain User-Agent header etc.
Check the contents of this file to see if you can gather anything from it.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:10/11/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><curl><download>', 'Title': 'php Curl download tbz file?', 'ViewCount': 331, 'id': 7721640, 'user_AboutMe': '<p>Database and Server/BackEnd Engineer.</p>\n', 'user_AccountId': 460486.0, 'user_DisplayName': 'Astha', 'user_Reputation': 1688.0, 'user_Views': 234.0, 'user_id': 861602.0}}