开发者

How to save file from internet with PHP?

开发者 https://www.devze.com 2023-04-02 10:10 出处:网络
For example, I have a link \"http://www.abcd.com/folder/1.fl开发者_StackOverflowv\" 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

For example, I have a link "http://www.abcd.com/folder/1.fl开发者_StackOverflowv" 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.


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


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


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>


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号