Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP读取文件的最快方法是什么?_Php_File_File Io_Fopen_Fgets - Fatal编程技术网

用PHP读取文件的最快方法是什么?

用PHP读取文件的最快方法是什么?,php,file,file-io,fopen,fgets,Php,File,File Io,Fopen,Fgets,用PHP读取文件的最快方法是什么?具体来说,我正在读取一个url,我正在使用fgets读取一个文件,url的大小约为1MB,读取5个url最多需要20秒。我只得到一行字符串,它位于文件的结尾部分。实际上,我使用fseek将指针移动到url的末尾,但它只对文件(而不是url)有效。有什么好主意吗 这是我的示例代码 $fp=fopen("http://url.com", "r"); if(is_bool($fp)){ exit; } while(!feof($fp)) { $data

用PHP读取文件的最快方法是什么?具体来说,我正在读取一个url,我正在使用fgets读取一个文件,url的大小约为1MB,读取5个url最多需要20秒。我只得到一行字符串,它位于文件的结尾部分。实际上,我使用fseek将指针移动到url的末尾,但它只对文件(而不是url)有效。有什么好主意吗

这是我的示例代码

$fp=fopen("http://url.com", "r");
if(is_bool($fp)){
    exit;
}
while(!feof($fp)) {
    $data = fgets($fp);
    if($data=="this is what i've wanted")
    {
        // codes...
    }
}
fclose($fp);

在这种情况下,你不能跳过所有的内容。网络传输的工作方式是定义内容长度,并且必须在需要的部分之前阅读所有内容

简而言之,你不能跳过它。只要把它读进去,抓住你需要的,继续生活


*注意:如果资源支持部分内容和范围请求,则可以。不太可能。

使用CURL获取远程url:

function curlRequest($url)
{

    $curl = curl_init();
    curl_setopt($curl,  CURLOPT_URL,                $url);
    curl_setopt($curl,  CURLOPT_RETURNTRANSFER,     true);
    curl_setopt($curl,  CURLOPT_FOLLOWLOCATION,     true);
    curl_setopt($curl,  CURLOPT_BINARYTRANSFER,     true);
    curl_setopt($curl,  CURLOPT_ENCODING,           true);
    curl_setopt($curl,  CURLOPT_HEADER,             false);

    $a = new stdclass;
    $a->body            = curl_exec         ($curl);
    $a->status          = curl_getinfo      ($curl,     CURLINFO_HTTP_CODE);
    $a->effectiveURL    = curl_getinfo      ($curl,     CURLINFO_EFFECTIVE_URL);

    curl_close ($curl);

    return $a;
}

您可以使用支持恢复下载的cUrl仅检索文件的最后一部分:

function retrieve_tail($remoteFile, $localFile = null, $bytes = 1000) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RANGE, intval($bytes)."-");
  $fp = fopen($localFile, "w+");
  curl_setopt($ch, CURLOPT_FILE, $fp);
  $result = curl_exec($ch);
  curl_close($ch);
  fclose($fp);
  return $result;
}
然后打电话:

  $result = retrieve_tail("http://url.com", "local_copy.txt", 20000);
  print_r($result);
您将在指定的本地文件上获得所需的内容。另外,你可以设置

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

直接获取内容。

您可以尝试
file\u get\u contents
。不过,我不确定它是否会更快