午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

PHP下載采集圖片到本地的方法詳解【可忽略ssl認(rèn)證】

瀏覽:239日期:2022-06-13 16:28:01
readfile和file_put_contents下載遠(yuǎn)程圖片到本地<?phpfunction download_image($pic_url){ $time = time(); $pic_local_path = dirname(__FILE__) . '/cache'; $pic_local = $pic_local_path . '/' . $time; if (!file_exists($pic_local_path)) {mkdir($pic_local_path, 0777);@chmod($pic_local_path, 0777); } ob_start(); //打開輸出 readfile($pic_url); //輸出圖片文件 $img = ob_get_contents(); //得到瀏覽器輸出 ob_end_clean(); //清除輸出并關(guān)閉 file_put_contents($pic_local, $img); return $pic_local;}curl下載遠(yuǎn)程圖片到本地<?php$ch = curl_init();$fp=fopen('./girl.jpg', 'w');curl_setopt($ch, CURLOPT_URL, 'https://img02.sogoucdn.com/app/a/100520091/20181209114105');curl_setopt($ch, CURLOPT_FAILONERROR, true);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($ch, CURLOPT_FILE, $fp); $output = curl_exec($ch);$info = curl_getinfo($ch);$error = curl_error($ch);fclose($fp);$size = filesize('./girl.jpg');if ($size != $info['size_download']) {echo '下載失敗';echo $error;} else {echo '下載成功';}curl_close($ch);/** * 下載遠(yuǎn)程圖片到本地 * * @param string $url 遠(yuǎn)程文件地址 * @param string $filename 保存后的文件名(為空時(shí)則為隨機(jī)生成的文件名,否則為原文件名) * @param array $fileType 允許的文件類型 * @param string $dirName 文件保存的路徑(路徑其余部分根據(jù)時(shí)間系統(tǒng)自動(dòng)生成) * @param int $type 遠(yuǎn)程獲取文件的方式 * @return json 返回文件名、文件的保存路徑 * @author blog.snsgou.com */function download_image($url, $fileName = '', $dirName, $fileType = array('jpg', 'gif', 'png'), $type = 1){ if ($url == '') {return false; } // 獲取文件原文件名 $defaultFileName = basename($url); // 獲取文件類型 $suffix = substr(strrchr($url, '.'), 1); if (!in_array($suffix, $fileType)) {return false; } // 設(shè)置保存后的文件名 $fileName = $fileName == '' ? time() . rand(0, 9) . '.' . $suffix : $defaultFileName; // 獲取遠(yuǎn)程文件資源 if ($type) {$ch = curl_init();$timeout = 30;curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);$file = curl_exec($ch);curl_close($ch); } else {ob_start();readfile($url);$file = ob_get_contents();ob_end_clean(); } // 設(shè)置文件保存路徑 //$dirName = $dirName . '/' . date('Y', time()) . '/' . date('m', time()) . '/' . date('d', time()); $dirName = $dirName . '/' . date('Ym', time()); if (!file_exists($dirName)) {mkdir($dirName, 0777, true); } // 保存文件 $res = fopen($dirName . '/' . $fileName, 'a'); fwrite($res, $file); fclose($res); return array('fileName' => $fileName,'saveDir' => $dirName );}PHP讀寫大 二進(jìn)制 文件

不必申請(qǐng)很大內(nèi)存(fopen、fread、fwrite、fclose)

<?php/** * 讀寫大二進(jìn)制文件,不必申請(qǐng)很大內(nèi)存 * 只有讀取到內(nèi)容才創(chuàng)建文件 * 保證目錄可寫 * * @param string $srcPath 源文件路徑 * @param string $dstPath 目標(biāo)文件路徑 * @return bool */function fetch_big_file($srcPath, $dstPath){ set_time_limit(0); // 設(shè)置腳本執(zhí)行時(shí)間無限長 if (!$fpSrc = fopen($srcPath, 'rb')) {return false; } $isWriteFileOpen = false; // 寫文件 是否已打開? do {$data = fread($fpSrc, 8192); // 每次讀取 8*1024個(gè)字節(jié)if (!$data){ break;}else if (!$isWriteFileOpen){ // 第一次讀取文件,并且有內(nèi)容,才創(chuàng)建文件 $fpDst = fopen($dstPath, 'wb'); $isWriteFileOpen = true; fwrite($fpDst, $data);}else{ // 寫入 fwrite($fpDst, $data);} } while (true); fclose($fpSrc); fclose($fpDst); return true;}$srcPath = 'd:/big.pdf';$dstPath = 'Z:/big.pdf';fetch_big_file($srcPath, $dstPath);echo 'success';注:代碼說明curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

可忽略ssl認(rèn)證,對(duì)于圖片https協(xié)議失效的情況可以忽略驗(yàn)證,正常訪問。

同樣的,使用 file_get_contents 函數(shù)忽略https認(rèn)證的話可以使用如何代碼實(shí)現(xiàn):

$url = 'https://img02.sogoucdn.com/app/a/100520091/20181209114105';$stream_opts = [? ? 'ssl' => [? ? ? ? 'verify_peer'=>false,? ? ? ? 'verify_peer_name'=>false,? ? ]];$cdata = file_get_contents($url,false, stream_context_create($stream_opts));
標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 四虎国产成人永久精品免费 | 精品久久三级 | 一级黄色在线 | 在线播放黄色网址 | 2021av| 男操女免费视频 | 中文字幕永久在线视频 | 成人精品二区 | 欧美激情亚洲色图 | 成人在线免费播放 | 欧美日韩a | 国产精品福利小视频 | 日韩在线视频二区 | 69夫妻乐园 | 四虎成人在线 | 国内精品免费视频 | 欧美wwww| 一区二区精品视频在线观看 | 免费成人黄色网址 | 伊人爱爱网 | 国产高清一区 | 91精品国产综合久久精品图片 | 久久久久久精 | 夜夜爽天天操 | 亚洲精品网站在线播放gif | 黄网址在线 | 91精品久久久久久综合五月天 | 中国美女毛片 | 午夜视频a | 成人中文字幕在线观看 | 久久精品二区 | 日韩三区在线 | 国产乱淫av一区二区三区 | 黄色综合网站 | 国内av在线| 久久国产精品99久久人人澡 | 天天躁日日躁狠狠躁av麻豆 | 欧美xx视频| 全部免费毛片在线播放高潮 | 日韩欧美国产高清 | 在线看日韩av|