Shell

推荐列表 站点导航

当前位置:首页 > 脚本编程 > Shell >

PHP如何下载远程文件到指定目录

来源:网络整理  作者:网友投稿  发布时间:2020-12-28 23:56
jquery中文网为您提供PHP如何下载远程文件到指定目录等资源,欢迎您收藏本站,我们将为您提供最新的PHP如何下载远...
PHP下载远程文件到指定目录

PHP用curl可以轻松实现下载远程文件到指定目录:

<?php class Download { public static function get($url, $file) { return file_put_contents($file, file_get_contents($url)); } public static function curlGet($url, $file) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $file_content = curl_exec($ch); curl_close($ch); $downloaded_file = fopen($file, 'w'); fwrite($downloaded_file, $file_content); fclose($downloaded_file); } public static function openGet($url, $file) { $in = fopen($url, "rb"); $out = fopen($file, "wb"); while ($chunk = fread($in,8192)) { fwrite($out, $chunk, 8192); } fclose($in); fclose($out); } /** * * 创建目录,支持递归创建目录 * @param String $dirName 要创建的目录 * @param int $mode 目录权限 */ public static function smkdir($dirName , $mode = 0777) { $dirs = explode('/' , str_replace('\\' , '/' , $dirName)); $dir = ''; foreach ($dirs as $part) { $dir.=$part . '/'; if ( ! is_dir($dir) && strlen($dir) > 0) { if ( ! mkdir($dir , $mode)) { return false; } if ( ! chmod($dir , $mode)) { return false; } } } return true; } }

更多PHP相关知识,请访问PHP教程!

以上就是PHP如何下载远程文件到指定目录的详细内容,更多请关注jquery中文网其它相关文章!

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://www.juheyunku.com/jiaob/shell/10449.shtml

相关文章
最新文章
Centos下PHP5升级为PHP7的方法 Centos下PHP5升级为PHP7的方法

时间:2021-01-03

在php当中常量和变量的区 在php当中常量和变量的区

时间:2020-12-29

PHP中经典的四大排序算法 PHP中经典的四大排序算法

时间:2020-12-29

dw怎么运行php文件? dw怎么运行php文件?

时间:2020-12-29

PHP PHP_EOL 换行符 PHP PHP_EOL 换行符

时间:2020-12-29

Python3爬虫进阶:MongoDB存储 Python3爬虫进阶:MongoDB存储

时间:2020-12-29

python如何运行一个python程 python如何运行一个python程

时间:2020-12-29

用PHP写一个计算器(附完 用PHP写一个计算器(附完

时间:2020-12-29

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

PHP如何下载远程文件到指定目录

2020-12-28 编辑:网友投稿

PHP下载远程文件到指定目录

PHP用curl可以轻松实现下载远程文件到指定目录:

<?php class Download { public static function get($url, $file) { return file_put_contents($file, file_get_contents($url)); } public static function curlGet($url, $file) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $file_content = curl_exec($ch); curl_close($ch); $downloaded_file = fopen($file, 'w'); fwrite($downloaded_file, $file_content); fclose($downloaded_file); } public static function openGet($url, $file) { $in = fopen($url, "rb"); $out = fopen($file, "wb"); while ($chunk = fread($in,8192)) { fwrite($out, $chunk, 8192); } fclose($in); fclose($out); } /** * * 创建目录,支持递归创建目录 * @param String $dirName 要创建的目录 * @param int $mode 目录权限 */ public static function smkdir($dirName , $mode = 0777) { $dirs = explode('/' , str_replace('\\' , '/' , $dirName)); $dir = ''; foreach ($dirs as $part) { $dir.=$part . '/'; if ( ! is_dir($dir) && strlen($dir) > 0) { if ( ! mkdir($dir , $mode)) { return false; } if ( ! chmod($dir , $mode)) { return false; } } } return true; } }

更多PHP相关知识,请访问PHP教程!

以上就是PHP如何下载远程文件到指定目录的详细内容,更多请关注jquery中文网其它相关文章!

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://www.juheyunku.com/jiaob/shell/10449.shtml

相关文章

风云图片

推荐阅读

返回Shell频道首页