How to download file through URL using PHP

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
iqueen
Forum Newbie
Posts: 8
Joined: Fri Jun 17, 2005 2:46 am

How to download file through URL using PHP

Post by iqueen »

How to use PHP to download a file/picture by given a URL. And save the downloaded file to disk.
Thanks.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

try the manual, it is always a good bet.

file_get_contents does that

NOTE: I'm faster than lightning :P
if you were faster then light, techniqally (dont know how to spell that) you would be moving through time...
... well, I do believe I'm a bit older now than I was when I first posted this post :P
Last edited by Syranide on Wed Jun 22, 2005 4:10 am, edited 2 times in total.
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

if you were faster then light, techniqally (dont know how to spell that) you would be moving through time...
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

i searched the manual and got this

http://us3.php.net/fread
asmie
Forum Newbie
Posts: 11
Joined: Mon Jun 20, 2005 8:45 am

File download

Post by asmie »

To download a file you will have to specify the header information in your script.
It will be like this :

Code: Select all

header("Content-type: text/plain"); //here you will have to mention the file type 
header("Content-Length: 1024");     // this is the filesize 
header("Content-Disposition: attachment; filename=test.txt"); // filename is the name you want to store the file as.It can also be any other name

include 'test.txt'; // this is the file that you want to download
Yenjoyy ! ;)

A s m i e
iqueen
Forum Newbie
Posts: 8
Joined: Fri Jun 17, 2005 2:46 am

Post by iqueen »

method_man wrote:i searched the manual and got this

http://us3.php.net/fread
can fread do this:
<?php
// get contents of a file into a string
$filename = "http://www.usa.com/index.html";
$handle = fopen($filename, "r");
...
iqueen
Forum Newbie
Posts: 8
Joined: Fri Jun 17, 2005 2:46 am

Re: File download

Post by iqueen »

asmie wrote:To download a file you will have to specify the header information in your script.
It will be like this :

Code: Select all

header("Content-type: text/plain"); //here you will have to mention the file type 
header("Content-Length: 1024");     // this is the filesize 
header("Content-Disposition: attachment; filename=test.txt"); // filename is the name you want to store the file as.It can also be any other name

include 'test.txt'; // this is the file that you want to download
Yenjoyy ! ;)

A s m i e
I just want to down load a file using PHP.
Using php to retrieve a file by given a url:http://www.usa.com/index.html,
save the index.html to disk.
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

download is one word...
asmie
Forum Newbie
Posts: 11
Joined: Mon Jun 20, 2005 8:45 am

Post by asmie »

Do you want an end user to be able to download files from your site ?
Something like downloading a mail attachment that we can download to our disk??

If yes then :
To download using an url you will have to specify like this :

http://www.usa.com/index.php

In index.php u can paste the code i had given in my previous post.
If the file you want to download is varying then you will have to change the code in the header.
Instead of

include 'test.txt';

You have to write the filename that u want to download.



A s m i e
Post Reply