导航:首页 > 编程语言 > phpcurl下载远程文件

phpcurl下载远程文件

发布时间:2022-10-24 21:36:35

Ⅰ 用php如何获取远程的xls表格文件地址,并将它下载到本地

写段伪代码给你:

//先获取远程网页的源代码
$html=file_get_contents(http://mp3..com/歌曲播放页.html);

//用正则表达式分析源代码中的资源链接
$link=preg_match_all(正则)...

//读取资源文件
$bin=file_get_contents(http://mp3..com/时间都去哪了.mp3);

//保存资源文件到本地
$fp=fopen(时间都去哪了.mp3,wb)
$fp.writh($bin);
$fp.close();


先说好,不要让我写完整的源代码,因为这里还涉及一些细节,比如说,你才提供的 xls ,就是需要登录的,那么你还要实现模拟登录。

有些网站的资源链接有各种限制,需要你慢慢去深入。

Ⅱ php保存远程文件到文件夹

具体看步骤吧:
function getFile($url,$save_dir='',$filename='',$type=0){
if(trim($url)==''){
return false;
}
if(trim($save_dir)==''){
$save_dir='./';
}
if(0!==strrpos($save_dir,'/')){
$save_dir.='/';
}
//创建保存目录
if(!file_exists($save_dir)&&!mkdir($save_dir,0777,true)){
return false;
}
//获取远程文件所采用的方法
if($type){
$ch=curl_init();
$timeout=5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$content=curl_exec($ch);
curl_close($ch);
}else{
ob_start();
readfile($url);
$content=ob_get_contents();
ob_end_clean();
}
$size=strlen($content);
//文件大小
$fp2=@fopen($save_dir.$filename,'a');
fwrite($fp2,$content);
fclose($fp2);
unset($content,$url);
return array('file_name'=>$filename,'save_path'=>$save_dir.$filename);
}
getFile($url,$save_dir,$filename,1)//调用

Ⅲ PHP CURL 获取远程数据下载

这样做肯定是用的你的带宽,是把文件下载到你的服务器上,然后再下载给客户端。

有两条路你可以去试试看,我没做过:一是setcookie指定域名是那个网站,然后转向:
setcookie ($cname ,$cvalue ,$expire ,$path , $host);
header('location: $url");

另外一个方法类似,好像有个P3P可以传递COOKIE,需要你自己查资料:
setcookie ($cname ,$cvalue);
header('P3P: ....');
header('location: $url");

第二个办法应该是可以的,陶宝和开心网都在用这样的技术,陶宝有许多域名,一次登录后都可以使用,就是利用P3P实现的COOKIE传递。

Ⅳ php写curl下载文件 不是下载到服务器 让浏览器弹出下载文件,在本地下载 求高手解答

这样的用header吧
$file=‘下载地址’;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}

Ⅳ windows 的php环境下 用curl远程下载速度慢

使用fsockopen

Ⅵ PHP 通过curl下载ftp文件,怎么设置端口号

ftp://172.19.71.63:8080/Flipped.2010.BluRay.720p.DTS.x264-CHD.sample.mkv

Ⅶ thinkphp的http::download怎么做下载文件

一、使用curlDownload 采集远程文件

/** * 采集远程文件 * @access public * @param string $remote 远程文件名 * @param string $local 本地保存文件名 * @return mixed */static public function curlDownload($remote,$local) {
$cp = curl_init($remote);
$fp = fopen($local,"w"); curl_setopt($cp, CURLOPT_FILE, $fp); curl_setopt($cp, CURLOPT_HEADER, 0); curl_exec($cp); curl_close($cp); fclose($fp);
}

调用:

$Http = new OrgNetHttp();
$Http::curlDownload("m/.jpg", "./Public/file/1.jpg");

二、使用download 下载文件

/** * 下载文件 * 可以指定下载显示的文件名,并自动发送相应的Header信息 * 如果指定了content参数,则下载该参数的内容 * @static * @access public * @param string $filename 下载文件名 * @param string $showname 下载显示的文件名 * @param string $content 下载的内容 * @param integer $expire 下载内容浏览器缓存时间 * @return void */ static public function download ($filename, $showname='',$content='',$expire=180) { if(is_file($filename)) {
$length = filesize($filename);
}elseif(is_file(UPLOAD_PATH.$filename)) { $filename = UPLOAD_PATH.$filename;
$length = filesize($filename);
}elseif($content != '') {
$length = strlen($content);
}else { E($filename.L('下载文件不存在!'));
} if(empty($showname)) { $showname = $filename;
} $showname = basename($showname);if(!empty($filename)) {
$finfo = new finfo(FILEINFO_MIME);
$type = $finfo->file($filename);
}else{
$type = "application/octet-stream";
} //发送Http Header信息 开始下载 header("Pragma: public"); header("Cache-control: max-age=".$expire); //header('Cache-Control: no-store, no-cache, must-revalidate'); header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT"); header("Content-Disposition: attachment; filename=".$showname); header("Content-Length: ".$length); header("Content-type: ".$type); header('Content-Encoding: none'); header("Content-Transfer-Encoding: binary" ); if($content == '' ) { readfile($filename);
}else { echo($content);
} exit();
}

调用前,首先要确定有没有开启php_fileinfo扩展,没有的话,则会报错。。

wampserver开启方式:


选择php_fileinfo就行了

调用:

$Http = new OrgNetHttp();$filename="Public/file/test.doc";
$showname="test.doc";
$content = "this"; // 表示下载的文件内容只有this$Http::download($filename, $showname, $content);

谢谢关注~

Ⅷ php curl get 下载远程zip文件保存在本地例子

<?php

if($_POST['submit']){
$url=$_POST['url']; //取得提交过来的地址http://hu60.cn/wap/0wap/addown.php/fetion_sms.zip
$url=urldecode($url);
$fname=basename("$url"); //返回路径中的文件名部分 fetion_sms.zip
$str_name=pathinfo($fname); //以数组的形式返回文件路径的信息
$extname=strtolower($str_name['extension']); //把扩展名转换成小写
//$uptypes=explode(",",$forum_upload); //取得可以上传的文件格式
//$size=getFileSize($url);

$time=date("Ymd",time());

$upload_dir="./upload/";//上传的路径
$file_name=$time.rand(1000,9999).'.'.$fname;
$dir=$upload_dir.$file_name;//创建上传目录

//判断目录是否存在 不存在则创建
if(!file_exists($upload_dir)){
mkdir($upload_dir,0777,true);
}

$contents=curl_download($url,$dir);

if($contents){
echo "下载成功";
}else{
echo "下载失败";
}

}

function curl_download($url, $dir) {
$ch = curl_init($url);
$fp = fopen($dir, "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res=curl_exec($ch);
curl_close($ch);
fclose($fp);
return $res;
}

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>远程下载文件</title>
<form name="upform" method="post" action="" enctype='multipart/form-data'>
<input name='url' type='text' size='20'/>
<input type='submit' name='submit' value='远程下载'/>
</form>
</body>
</html>

Ⅸ curl获取远程图片时,如何设置本地保存路径

设置保存路径

define('IMAGE_DIR', 'c:\\xampp\\htdocs\\scraper\\image\\');

保存图片函数。

$imageUrl = 你要的图片的url
$imageType = 你要的图片保存的格式

saveImage($imageUrl, $imageType = 'IMAGETYPE_GIF') {
if (!file_exists(IMAGE_DIR)) {
mkdir(IMAGE_DIR, 0777, true);
}

if( $imageType === IMAGETYPE_JPEG ) {
$fileExt = 'jpg';
} elseif ( $imageType === IMAGETYPE_GIF ) {
$fileExt = 'gif';
} elseif ( $imageType === IMAGETYPE_PNG ) {
$fileExt = 'png';
}

$newImageName = md5($imageUrl). '.' . $fileExt;
$image = new Image();
$image->load($imageUrl);
$image->resizeToWidth(100);
$image->save( IMAGE_DIR . $newImageName, $imageType );
return $newImageName;
}

这是我的图片类,保存前可转换格式,图片大小。

<?php

class Image {
private $_image;
private $_imageFormat;

public function load($imageFile) {
$imageInfo = getImageSize($imageFile);
$this->_imageFormat = $imageInfo[2];
if( $this->_imageFormat === IMAGETYPE_JPEG ) {
$this->_image = imagecreatefromjpeg($imageFile);
} elseif( $this->_imageFormat === IMAGETYPE_GIF ) {
$this->_image = imagecreatefromgif($imageFile);
} elseif( $this->_imageFormat === IMAGETYPE_PNG ) {
$this->_image = imagecreatefrompng($imageFile);
}
}

public function save($imageFile, $_imageFormat=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $_imageFormat == IMAGETYPE_JPEG ) {
imagejpeg($this->_image,$imageFile,$compression);
} elseif ( $_imageFormat == IMAGETYPE_GIF ) {
imagegif($this->_image,$imageFile);
} elseif ( $_imageFormat == IMAGETYPE_PNG ) {
imagepng($this->_image,$imageFile);
}
if( $permissions != null) {
chmod($imageFile,$permissions);
}
}

public function getWidth() {
return imagesx($this->_image);
}

public function getHeight() {
return imagesy($this->_image);
}

public function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}

public function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}

public function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}

private function resize($width, $height) {
$newImage = imagecreatetruecolor($width, $height);
imageresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->_image = $newImage;
}

}

?>

Ⅹ php curl 下载保存时文件类型怎么确定

curl,一般都用来抓取网页内容。
返回值是字符流
你把它保存成.html后缀就行了

阅读全文

与phpcurl下载远程文件相关的资料

热点内容
unity资源包在哪个文件夹 浏览:702
阿里云服务器远程链接不成功 浏览:482
文件系统pdf 浏览:762
原神安卓区服什么意思 浏览:34
贝壳app怎么线上发布 浏览:157
如何挑选安卓系统机顶盒 浏览:53
安卓快充使用有什么注意事项 浏览:909
黑马程序员的云计算网课 浏览:946
endnotestyle文件夹怎么导入 浏览:460
讲解少儿编程演讲会开头 浏览:424
思科交换机基础命令 浏览:497
便签可以设置加密吗 浏览:339
免费漫画app怎么看书 浏览:27
华为笔记本电脑怎么安装抖音app 浏览:412
阿里云国际版试用的服务器怎么搞 浏览:895
java正则表达式工具 浏览:160
oa服务器怎么设置ftp 浏览:10
安卓如何安装obb 浏览:442
QQ聊天记录journal文件夹 浏览:118
苹果公司云服务器地址 浏览:85