user916350
user916350

Reputation: 97

How to save file from internet with PHP?

For example, I have a link "http://www.abcd.com/folder/1.flv" and I need save this file in my hard disk. I must do that by PHP, but my knowledge of PHP is little. Tell me please how I can do that.

Upvotes: 0

Views: 2758

Answers (4)

poostchi
poostchi

Reputation: 422

file_put_contents($localURL, file_get_contents($remoteURL));

Upvotes: 2

Daniel Fekete
Daniel Fekete

Reputation: 5028

<?
copy("http://www.abcd.com/folder/1.flv", "1.flv");
?>

See http://php.net/manual/en/function.copy

Upvotes: 0

cwallenpoole
cwallenpoole

Reputation: 82058

Well, for smaller files, you can use file_get_contents with file_put_contents, but in this case, (because it is an flv), you may wish to use two files:

$rd = fopen("http://www.abcd.com/folder/1.flv", 'r');
$wt = fopen("<local path to write to>", "w");
// read a line from the url (8192 is a standard chunk size)
while( FALSE !== ( $ln = fread( $rd, 8192 ) ) )
{
    // write it locally
    fwrite( $wt, $ln );
    // rinse, repeat until file is done
}
fclose( $wt ); // close the local file.
fclose( $rd ); // close the remote stream

Upvotes: 3

RET
RET

Reputation: 9188

Using file_get_contents is all very well if you're scripting a download, but if you're referring to forcing a link on a page to be a download rather than embedded content when a user clicks on it, that's a very different thing. It's not clear from your question.

If it's the latter, you don't have to do this via PHP, you could set up a rule in your .htaccess file or your apache's .conf file, depending on the access you have at your disposal:

<FilesMatch "\.flv">
    Header set Content-Disposition attachment
    Header set Content-Type application/x-unknown
</FilesMatch>

Upvotes: 0

Related Questions