how to download a file in php? [duplicate]
Possible Duplicate:
How to download a file to server PHP?
hello,
i'm trying to write a simple php code to download a file from a server.....can someone guide me how to do it....i've trying to find some guide but each of them is so confusing that i can make anything out of it.......
any help will be highly appreciated....
If you're trying to download a simple text file, you can use file(), or file_get_contents():
file('http://url/to/file.txt') will return an array of lines
file_get_contents('http://url/to/file.txt') will return the file as a string
Alternatively, when the data type is binary for example, you can use the following snippet, found at php.net
<?php
$filename = "/usr/local/something.txt"; // This can also be an url to an external file
$handle = fopen($filename, "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
Now you could for example write the contents to a file.
Hope this helps.
[Edit] Apologies, I pasted the wrong snippet, this should be the right one
The manual page on file_get_contents()
provides a full working example.
If you encounter problems with it, you can ask a specific question.
Just make use of the php copy function:
if (!copy('http://www.example.com/file.jpg', '/path/to/dir/newfile.jpg'))
{
echo "failed to copy $file...\n";
}
精彩评论