首页 文章

用php下载excel文件

提问于
浏览
0

我需要从外部链接下载excel文件 .

我写了两个代码,都出错了:

第一 :

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

运行此脚本后,我无法使用我的test.xlsx文件,并且得到了excel错误:

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.

第二个 :

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

我又得到了同样的错误 . 当我试图从浏览器打开该链接时,xlsx文件会自动下载但我无法从代码中下载该链接,PLZ帮助

1 回答

  • 0

    我对这个问题的理解是这样的

    yoursite www.yoursite.com

    站点文件www.sitewithfile.com/path/file.xlxs

    你使用链接的东西

    <a href='www.sitewithfile.com/path/file.xlxs' target='__blank'>Download</a>
    

相关问题