Download excel file from non static URL

hi

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

i wanna download excel file from below URL

http://www.tsetmc.com/tse/data/Export-x … 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.


<?php
$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 = '[http://www.tsetmc.com/tse/data/Export-x ... adeLastDay](http://www.tsetmc.com/tse/data/Export-xls.aspx?a=TradeLastDay)';
$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) {
fclose($file);
}

if ($newf) {
fclose($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:

<?php DECLARE(STRICT_TYPES=1);
error_reporting(-1);
ini_set('display_errors', '0');
// ONLY IF LOCALHOST
ini_set('display_errors', '1');

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

//===============================================
function curlGetFile(
  string $src, 
  string $dst
)
:bool
{
  $result = FALSE;

  $ok   = @chmod(  $dst, 0777 );
  set_time_limit(99);
    $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
    fclose($fd);
  curl_close($ch);

  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

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

thanks
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, 'http://www.tsetmc.com/tse/data/Export-xls.aspx?a=TradeLastDay' );
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.

3 Likes

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)';

AnyTime

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