Page 1 of 1

How to download file through URL using PHP

Posted: Wed Jun 22, 2005 4:02 am
by iqueen
How to use PHP to download a file/picture by given a URL. And save the downloaded file to disk.
Thanks.

Posted: Wed Jun 22, 2005 4:06 am
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

Posted: Wed Jun 22, 2005 4:08 am
by method_man
if you were faster then light, techniqally (dont know how to spell that) you would be moving through time...

Posted: Wed Jun 22, 2005 4:09 am
by method_man
i searched the manual and got this

http://us3.php.net/fread

File download

Posted: Wed Jun 22, 2005 4:15 am
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

Posted: Wed Jun 22, 2005 4:16 am
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");
...

Re: File download

Posted: Wed Jun 22, 2005 4:19 am
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.

Posted: Wed Jun 22, 2005 4:28 am
by method_man
download is one word...

Posted: Wed Jun 22, 2005 5:02 am
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