saving a pdf file from web

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mehnaz
Forum Newbie
Posts: 20
Joined: Mon Jun 02, 2008 7:49 pm

saving a pdf file from web

Post by Mehnaz »

Hi

I have some an array of some pdf links like this
$filename ="http://www.cdc.gov/nchs/data/ad/ad389.pdf";

I want to use pdftotext on it. But for this I think I need to save these files on my system. Please give me any clue how can I do this. ie. how I can save thiese files temporarly . A piece of code of would be highly appreciated.

Thanks in advance

Mehnaz
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: saving a pdf file from web

Post by McInfo »

Use file_get_contents() to read the file from the remote server. Then use file_put_contents() to save the contents to a file on your server. If you want the saved file to have the same name as the original, use basename().

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 3:59 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: saving a pdf file from web

Post by jackpf »

If they're on another domain I think curl would be a better option. I'm not sure if allow url fopen affects file_get_contents()...
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: saving a pdf file from web

Post by McInfo »

I tested my solution with the given PDF. I can personally guarantee that it works.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 3:59 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: saving a pdf file from web

Post by jackpf »

But you may have url fopen turned on, whereas many shared hosts do not.

Anyway, the only way to find out is for the op to test it :D
Mehnaz
Forum Newbie
Posts: 20
Joined: Mon Jun 02, 2008 7:49 pm

Re: saving a pdf file from web

Post by Mehnaz »

Thanks very much

I used file_get_contents() and file_put_contents() in this way

Code: Select all

$filename = "http://www.cdc.gov/nchs/data/ad/ad389.pdf";
 
 
$content = file_get_contents($filename);
$pdffilename = "temp.pdf";
 
file_put_contents($pdffilename, $content);
and it works perfectly.
Post Reply