加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php从URL下载文件的几种方法

发布时间:2020-05-25 02:32:33 所属栏目:PHP 来源:互联网
导读:本文章向大家介绍php从URL下载文件的几种方法,需要的朋友可以参考一下。

第一种方法:file_put_contents()

从PHP 5.1.0开始,file_put_contents()支持通过传递stream-handle作为$data参数逐个编写:

set_time_limit(0);

$file = file_get_contents('path of your file');

file_put_contents('file.ext',$file);

第二种方法:fopen 和fwrite

private function downloadFile($url,$path)

{

$newfname = $path;

$file = fopen ($url,'rb');

if ($file) {

$newf = fopen ($newfname,'wb');

if ($newf) {

while(!feof($file)) {

fwrite($newf,fread($file,1024 * 8),1024 * 8);

}

}

}

if ($file) {

fclose($file);

}

if ($newf) {

fclose($newf);

}

}

第三种方法:curl

function downloadUrlToFile($url,$outFileName)

{

if(is_file($url)) {

copy($url,$outFileName);

} else {

$options = array(

CURLOPT_FILE => fopen($outFileName,'w'),

CURLOPT_TIMEOUT => 28800,// set this to 8 hours so we dont timeout on big files

CURLOPT_URL => $url

);

$ch = curl_init();

curl_setopt_array($ch,$options);

curl_exec($ch);

curl_close($ch);

}

}

第四种方法:copy()

在php中使用一个简单的方法copy()

copy($source_url,$local_path_with_file_name);

注意:如果目标文件已存在,则将覆盖该文件

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读