1

I need to download an excel file from the external link.

i wrote 2 codes and both went wrong :

first one :

$url = "www.example.com";
$data = file_get_contents($url);
$destination = "test.xlsx";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file); 

after running this script, I can't my test.xlsx file, and got and excel error:

Excel cannot open this file, The file format or file extension is not valid. 
Verify that the file has not been corrupted and that the file extension matches the format of the file.

second one :

$ch = curl_init();
$targetFile = fopen( 'test.xlsx', 'w' );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:             
application/vnd.ms-excel"));
curl_setopt($ch, CURLOPT_URL,     
www.example.com');
curl_setopt( $ch, CURLOPT_FILE, $targetFile );
curl_exec( $ch );
fclose( $targetFile );

and again I got the same error. when I am trying to open that link from the browser , xlsx file downloads automatically but I cannot download that link from code, plz help

Pourya Fard
  • 57
  • 1
  • 9

1 Answers1

0

my understanding of the question is like

yoursite www.yoursite.com

site with file www.sitewithfile.com/path/file.xlxs

l thing you use a link

<a href='www.sitewithfile.com/path/file.xlxs' target='__blank'>Download</a>
Billy Watsy
  • 74
  • 1
  • 10