Download excel file from non static URL


firstly i didnt know how to name this topic by this type of URLs.

i wanna download excel file from below URL … adeLastDay

but when i open this link with browser every thing is OK and file start downloading but when i want to download it with below php code the downloaded file is corrupted.

$time_start = microtime(true);
ini_set('max_execution_time', 360);
echo "<br>" ;
//$file = file('web-graph.txt');
ini_set('memory_limit', '2048M');

// folder to save downloaded files to. must end with slash
$destination_folder = 'yes/';

$url = '[ ... adeLastDay](';
$newfname = $destination_folder . basename($url);

$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");

if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );

if ($file) {

if ($newf) {

$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Time: {$time}";
echo "<br>";


what is your point about this?

The manual says on fread:

  • if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.

So basically it may return more bytes than asked for. That, combined with limiting the number of bytes allowed to be written may cause missing chunks.

Try it like this:

fwrite($newf, fread($file, 1024 * 8));

Also, since you just want to read the entire file, the manual recommends using file_get_contents instead as it would perform better.

1 Like

I use PHP Curl(,) to download XLS files:

ini_set('display_errors', '0');
ini_set('display_errors', '1');

$src = '';
$dst = '';
$msg = 'Failed to download'; 
if ( curlGetFile( $src, $dst ) ) :
  $msg = 'Download Successful';
echo $msg; 

function curlGetFile(
  string $src, 
  string $dst
  $result = FALSE;

  $ok   = @chmod(  $dst, 0777 );
    $ch  = curl_init();
    curl_setopt($ch, CURLOPT_URL, $src);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $st  = curl_exec($ch);
    $fd  = fopen($dst, 'w');
    $ok  = fwrite($fd, $st); // return integer bytes written

  return  $ok > 0 ;
1 Like

this makes the file executable, which is a pretty bad idea to do for files download randomly from the internet. 0644 would be (a lot) better!

1 Like

but did not worked if you open the link you will see this link will redirect you to somewhere and then file will download.

your code can download the file but file is corrupted and didnt downloaded completely like my code.original file is 100 kb but downloaded file is 24 kb :frowning:

i dont know why this happening. this link makes me mixed up.i can download with browser when i open the link but with this code i cant…i think i didnt considered one thing here.

I had a quick search and came across this link which I briefly tried and could not get it to work:

1 Like

Thanks this one helped me to move one step forward. but still i cant get all of data. i can see the first row that is the name of columns,

i have below code for now

$ch = curl_init();
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

curl_setopt( $ch, CURLOPT_URL, '' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec ( $ch );
curl_close ( $ch );
echo $result;

Thanks i worked on it and this code works for me.

thanks a lot

1 Like

It would perhaps be good of you to post the working code, so that anyone reading this thread in the future can see the solution as well as the problem.


hi problem solved with adding useragent to curl request

$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';


1 Like

Thanks for the update. Strange that helped, as that was in your previous code where you still had the corrupted download.

1 Like

yes i think this problem was from server response

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.