php copy saves remote pdf but viewers throw error message

Hello,

Here is my code:

[php]<?php
$file = ‘http://example.com/example.pdf’;
$newfile = ‘c:\example.pdf’;

if (!copy($file, $newfile)) {
echo “failed to copy $file…\n”;
}
?>[/php]

The file saves just fine. When I try to open it I get an error from the reader (both Adobe and Nitro) that indicates that the file is corrupt. When I go to http://example.com/ and either click save target as or open the file in a reader then I can save it and open it later with no problems.

I have also tried cURL with the same results. Here’s the code:

[php]<?php
$url = ‘http://www.example.com/example.pdf’;
$path = ‘c:\example.pdf’;

$fp = fopen($path, 'w');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);

$data = curl_exec($ch);

curl_close($ch);
fclose($fp);

?>
[/php]
Can you help?

Sponsor our Newsletter | Privacy Policy | Terms of Service