0

I'm having issues downloading an excel file onto my web server from a URL using PHP. The file is publicly accessible on a Microsoft Sharepoint server and downloads automatically when accessed directly via the link. Here is my code:

$url = 'https://example.sharepoint.com/somefile.xlsx?download=1';
$file = file_put_contents('template.xlsx', file_get_contents($url));

The script successfully creates the template.xlsx file on the server, however the file itself is 0 bytes and when I try to open it I get an error along the lines of "Excel cannot open the file because the file format or file extension is invalid". What am I missing?

sverdon
  • 50
  • 5
  • I'm guessing the link redirects or points to a page that requires JS? What headers do you get with `curl -D - `? – Alex Howansky May 24 '22 at 17:55
  • I recommend use cURL to download file instead. See https://stackoverflow.com/questions/6409462/downloading-a-large-file-using-curl – vee May 24 '22 at 18:07
  • @AlexHowansky With some info redacted... HTTP/1.1 302 Found Cache-Control: private Content-Length: 517 Content-Type: text/html; charset=utf-8 Location: https://sharepoint.com/somefile.xlsx P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI" Set-Cookie: FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1ATwvU1A+; path=/; SameSite=None; secure; HttpOnly X-SharePointHealthScore: 1 X-MS-SPO-CookieValidator: sQ7YhnGeis9fooVHWZdO8h/x8DvNqgc6xlu+gISDPMUqcoghMO0mHlIx0mdU5L3BkP X-AspNet-Version: 4.0.30319 – sverdon May 24 '22 at 18:52
  • @AlexHowansky X-DataBoundary: None X-1DSCollectorUrl: https://mobile.events.data.microsoft.com/OneCollector/1.0/ X-AriaCollectorURL: https://browser.pipe.aria.microsoft.com/Collector/3.0/ SPRequestGuid: 3f9940a0-4045-d000-0d64-e62c63cd9263 request-id: 3f9940a0-4045-d000-0d64-e62c63cd9263 MS-CV: oECZP0VAANANZOYsY82SYw.0 Strict-Transport-Security: max-age=31536000 X-FRAME-OPTIONS: SAMEORIGIN Content-Security-Policy: frame-ancestors 'self' teams.microsoft.com *.teams.microsoft.com *.skype.com * – sverdon May 24 '22 at 18:55
  • @AlexHowansky X-Powered-By: ASP.NET MicrosoftSharePointTeamServices: 16.0.0.22504 X-Content-Type-Options: nosniff X-MS-InvokeApp: 1; RequireReadOnly X-Cache: CONFIG_NOCACHE X-MSEdge-Ref: Ref A: A0C7A23C205E468EBA2A53226CB3FFAF Ref B: DEN301000102047 Ref C: 2022-05-24T18:49:31Z – sverdon May 24 '22 at 18:55
  • @vee Initiating the download via cURL doesn't change the outcome. I still get an unreadable file. – sverdon May 24 '22 at 19:00
  • `HTTP/1.1 302 Found` That's a redirect. Try [this](https://stackoverflow.com/questions/4323985/how-to-get-the-real-url-after-file-get-contents-if-redirection-happens) to follow redirects. – Alex Howansky May 24 '22 at 19:24
  • @AlexHowansky I think we're getting closer.. it's now downloading a file, but the contents are HTML saying the file has moved to another location (yet the browser still initiates a download during a normal copy/paste): Object moved

    Object moved to here.

    – sverdon May 24 '22 at 22:21

0 Answers0