0

I'm trying to download a PDF file from a URL with php & curl. It has to return the file into a variable so i can put it in a base64 string.

If i run it, i keep getting a 404 "file not found" from the server (and if i post the url in my browser the file just downloads correctly).

$ch = curl_init();
$request_headers = array(
    "Content-Type:application/pdf",
    "Content-Disposition:attachment;filename='downloaded.pdf'",
);

curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');

$data = curl_exec($ch);
curl_close($ch);
Joachim
  • 11
  • 1
  • Try removing the attachment disposition to your header. – Martin Jan 31 '22 at 11:25
  • 1
    Does this answer your question? [How to download and save a file to local path using CURL](https://stackoverflow.com/questions/17827250/how-to-download-and-save-a-file-to-local-path-using-curl) – Martin Jan 31 '22 at 11:26
  • `Content-Disposition` is a RESPONSE header, you are using it as a REQUEST header - doesn't make sense. What are you actually trying to do? Where do you get 404? Then you disable SSL verification, you shouldn't be doing that. There is no reason in 2022 to turn it off ever. Then you might also need Cookies enabled to be allowed to download that file (`CURLOPT_COOKIEJAR`, `CURLOPT_COOKIEFILE`). – Daniel W. Jan 31 '22 at 11:26
  • 1
    Also if you provide the value for $url - we can potentially then use your code to debug it for you. – UrbanwarfareStudios Jan 31 '22 at 11:30
  • i have a url: https://foo.bar/somefile.pdf ($url), that pdf file i need and has to be stored in the $data var, afterwards i want this data var converted to base64 to store it somewhere in database. I get the 404 from https://foo.bar/ that the somefile.pdf doesn't exist – Joachim Jan 31 '22 at 11:31
  • Sounds like `somefile.pdf` is only temporarily or virtually avaiable - do you need to login to download it? Are there any cookies involved (in the browser, use the dev console in Application tab you have a list of cookies)? – Daniel W. Jan 31 '22 at 11:32
  • no it's permanently there. No login needed, no cookies in browser – Joachim Jan 31 '22 at 11:35

0 Answers0