導航:首頁 > 編程語言 > 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下載遠程文件相關的資料

熱點內容
購買的app會員怎麼退安卓手機 瀏覽:886
程序員的種類及名稱 瀏覽:290
美國程序員薪資 瀏覽:12
黑石通匯證券伺服器什麼時候到期 瀏覽:391
東方財富app里我的關注怎麼看 瀏覽:747
bm3d單反級降噪演算法 瀏覽:457
華為安卓機激活時間怎麼查詢 瀏覽:850
如何用優盤重裝伺服器系統 瀏覽:317
日本結婚三代演算法 瀏覽:920
皓強工具解壓步驟 瀏覽:690
部隊抗洪搶險命令範文 瀏覽:888
歐姆龍plc編程軟體使用教程 瀏覽:594
ai文件pdf 瀏覽:912
騰訊雲伺服器掛載混合雲 瀏覽:758
智能小車用什麼單片機 瀏覽:463
java怎麼給窗口關閉 瀏覽:940
列舉51單片機的定址方式 瀏覽:706
剪輯app怎麼寫長篇文字 瀏覽:400
app專屬流量過月租怎麼不更新 瀏覽:656
王者程序員都有誰 瀏覽:78