Downloading large files using PHP


I am using following code to download files from some remote server using php

//some php page parsing code $url = 'http://www.domain.com/'.$fn; $path = 'myfolder/'.$fn; $fp = fopen($path, 'w'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); fclose($fp); // some more code

but instead of downloading and saving the file in the directory it is showing the file contents (junk characters as file is zip) directly on the browser only.

I guess it might be an issue with header content, but not know exactly ...



I believe you need:


to make curl_exec() return the data, and:

$data = curl_exec($ch); fwrite($fp, $data);

to get the file actually written.


As mentioned in <a href="http://php.net/manual/en/function.curl-setopt.php" rel="nofollow">http://php.net/manual/en/function.curl-setopt.php</a> :

<strong>CURLOPT_RETURNTRANSFER: TRUE</strong> to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

So you can simply add this line before your curl_exec line:


and you will have the content in $data variable.


Use the following function that includes error handling.

// Download and save a file with curl function curl_dl_file($url, $dest, $opts = array()) { // Open the local file to save. Suppress warning // upon failure. $fp = @fopen($dest, 'w+'); if (!$fp) { $err_arr = error_get_last(); $error = $err_arr['message']; return $error; } // Set up curl for the download $ch = curl_init($url); if (!$ch) { $error = curl_error($ch); fclose($fp); return $error; } $opts[CURLOPT_FILE] = $fp; // Set up curl options $failed = !curl_setopt_array($ch, $opts); if ($failed) { $error = curl_error($ch); curl_close($ch); fclose($fp); return $error; } // Download the file $failed = !curl_exec($ch); if ($failed) { $error = curl_error($ch); curl_close($ch); fclose($fp); return $error; } // Close the curl handle. curl_close($ch); // Flush buffered data to the file $failed = !fflush($fp); if ($failed) { $err_arr = error_get_last(); $error = $err_arr['message']; fclose($fp); return $error; } // The file has been written successfully at this point. // Close the file pointer $failed = !fclose($fp); if (!$fp) { $err_arr = error_get_last(); $error = $err_arr['message']; return $error; } }


  • Laravel Socialite - google login failed “Missing required parameter: code”
  • Declaring a boost asio socket, acceptor and endpoint in a class headerfile
  • I want capture screen when any game is running with opengl [duplicate]
  • Open statement according to a file extension
  • PHP / CURL / ASP.NET / C# - Send files bigger than 9mb
  • PHP cURL Post request not working
  • mac OSX Unknown pseudo-op: .type
  • PHP cURL into authenticated site when already logged in
  • Why is fread() stopping at a string of 0s?
  • “Beveled” Shapes in Quartz 2D
  • How do I write to a text file in MATLAB?
  • reading off multiple CSV files
  • Eclipse 4.2, Mac OS X 10.8 (ML), and Java 6
  • Transfer from Server > Server > Client without download to server
  • Reading a whole line from file in c++
  • curl_init() has been disabled for security reasons
  • Using python to determine if a timestamp is under daylight savings time
  • Uploading image using SpringMVC 4.0 Multipart
  • Teradata Database 3130 Response limit exceeded?
  • How to retrieve a specific string from a specific list from a file with JSON in Python
  • css font-size and line-height not matching the baseline
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • Certain Arabic text gets incorrectly shown while other Arabic text gets showed normally?
  • custom string delimiters stringtemplate-4
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • How to implement Deep Linking in Roku SG application?
  • Configure Spring's MappingJacksonHttpMessageConverter
  • Validate jQuery plugin, field not required
  • AppleScript : find open tab in safari by name and open it
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Web.config system.webserver errors
  • ActiveRecord query for a count of new users by day
  • Checking free space on FTP server
  • Change Inet root folder for iis 7
  • Updated Ionic CLI but shows previous version (Windows)
  • When should I choose bucket sort over other sorting algorithms?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j