1,file_get_contents
<?php$content = file_get_contents($getPath);file_put_contents($savePath,$content);
<?php$content = file_get_contents($getPath);//file_get_contents超时处理$opts = array('http'=>array('method'=>"GET",'timeout'=>1,//单位秒));$cnt=0;while($cnt<3 && ($content=file_get_contents($getPath, false, stream_context_create($opts)))===FALSE) $cnt++;file_put_contents($savePath,$content);
<?php//乱码问题,特别是txt文件$file_content = mb_convert_encoding( file_get_contents($getPath, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );
2,fopen
<?php$handle = fopen($getPath, 'r');while(false != ($a = fread($handle, 10240))){//返回false表示已经读取到文件末尾$content .= $a;}fclose($handle);
<?php//分块读取并分块写入$handle = fopen($getPath, 'r'); //只读方式打开$toHandle = fopen($savePath,'a'); //写入方式打开,不存在则创建while(false != ($a = fread($handle, 10240))){//返回false表示已经读取到文件末尾fwrite($toHandle,$a);}fclose($toHandle);fclose($handle);
<?php$handle = fopen($getPath, "rb");$getData = fread($handle, 8096);$toHandle = fopen($savePath, "wb");while ($getData) {fwrite($file, $getData);$getData = fread($handle, 8096);}fclose($handle);fclose($toHandle);
<?php//$fileSize 文件大小$fp = fopen ( $file_path, "rb" ); // 打开文件$dstStream = fopen ( 'php://output', 'wb' ); //直接输出到浏览器$offset = 0;$chunksize = 4096; //分块大小while ( ! feof ( $fp ) && $offset < $fileSize ) {$last_size = $fileSize - $offset;if ($chunksize > $last_size && $last_size > 0) {$chunksize = $last_size;}$offset += stream_copy_to_stream ( $fp, $dstStream, $chunksize, $offset );}fclose ( $dstStream );fclose ( $fp );
3,fsockopen
参考来源:https://blog.csdn.net/navioo/article/details/82771663
<?php/*** 使用fsockopen发送URL请求* @param $url* @param $method: GET、POST等* @param array $params* @param array $header* @param int $timeout* @return array|bool*/function sendHttpRequest($url, $method = 'GET', $params = [], $header = [], $timeout = 30){$urlInfo = parse_url($url);if (isset($urlInfo['scheme']) && strcasecmp($urlInfo['scheme'], 'https') === 0) //HTTPS{$prefix = 'ssl://';$port = 443;}else{ //HTTP$prefix = 'tcp://';$port = isset($urlInfo['port']) ? $urlInfo['port'] : 80;}$host = $urlInfo['host'];$path = isset($urlInfo['path']) ? $urlInfo['path'] : '/';if(!empty($params) && is_array($params)){$params = http_build_query($params);}$contentType = '';$contentLength = '';$requestBody = '';if($method === 'GET'){$params = $params ? '?' . $params : '';$path .= $params;}else{$requestBody = $params;$contentType = "Content-Type: application/x-www-form-urlencoded\r\n";$contentLength = "Content-Length: " . strlen($requestBody) . "\r\n";}$auth = '';if(!empty($urlInfo['user'])){$auth = 'Authorization: Basic ' . base64_encode($urlInfo['user'] . ':' . $urlInfo['pass']) . "\r\n";}if($header && is_array($header)){$tmpString = '';foreach ($header as $key => $value){$tmpString .= $key . ': ' . $value . "\r\n";}$header = $tmpString;}else{$header = '';}$out = "$method $path HTTP/1.1\r\n";$out .= "Host: $host\r\n";$out .= $auth;$out .= $header;$out .= $contentType;$out .= $contentLength;$out .= "Connection: Close\r\n\r\n";$out .= $requestBody;//post发送数据前必须要有两个换行符\r\n$fp = fsockopen($prefix . $host, $port, $errno, $errstr, $timeout);if(!$fp){return false;}if(!fwrite($fp, $out)){return false;}$response = '';while(!feof($fp)){$response .= fread($fp, 1024);}if(!$response){return false;}fclose($fp);$separator = '/\r\n\r\n|\n\n|\r\r/';list($responseHeader, $responseBody) = preg_split($separator, $response, 2);$httpResponse = array('header' => $responseHeader,'body' => $responseBody);return $httpResponse;}
