獲取文件內容php
⑴ php如何獲取文件內容
PHP 中的file_get_contents() 函數可以實現
file_get_contents() 函數把整個文件讀入一個字元串中。
和 file() 一樣,不同的是 file_get_contents() 把文件讀入一個字元串。
file_get_contents() 函數是用於將文件的內容讀入到一個字元串中的首選方法。如果操作系統支持,還會使用內存映射技術來增強性能。
例如:
<?php
echo file_get_contents("test.txt");
?>
⑵ PHP讀取文件內容,和修改
1.
打開文件的模式有錯誤,改為下列的方式
2.
?php
3.
$filepath="num.txt";
4.
$file
=
fopen($filepath,"r");
5.
$idsum=fgets($file);
6.
fclose($file);
7.
$file2
=
fopen($filepath,"w");
8.
$idsum1=(integer)$idsum+1;
9.
echo
"idsum的值為".(integer)$idsum.";
idsum1的值為".$idsum1;
10.
fwrite($file2,$idsum1);
11.
fclose($file2);
12.
?
⑶ php讀取文本文件內容~
示例代碼1: 用file_get_contents 以get方式獲取內容
代碼如下:
<?php
$url='';
$html=file_get_contents($url);
//print_r($http_response_header);
ec($html);
printhr();
printarr($http_response_header);
printhr();
?>
示例代碼2: 用fopen打開url, 以get方式獲取內容
代碼如下:
<?
$fp=fopen($url,'r');
printarr(stream_get_meta_data($fp));
printhr();
while(!feof($fp)){
$result.=fgets($fp,1024);
}
echo"url body:$result";
printhr();
fclose($fp);
?>
示例代碼3:用file_get_contents函數,以post方式獲取url
代碼如下:
<?php
$data=array('foo'=>'bar');
$data=http_build_query($data);
$opts=array(
'http'=>array(
'method'=>'POST',
'header'=>"Content-type: application/x-www-form-urlencodedrn".
"Content-Length: ".strlen($data)."rn",
'content'=>$data
),
);
$context=stream_context_create($opts);
$html=file_get_contents('',false,$context);
echo$html;
?>
示例代碼4:用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body
代碼如下:
<?
functionget_url($url,$cookie=false){
$url=parse_url($url);
$query=$url[path]."?".$url[query];
ec("Query:".$query);
$fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
if(!$fp){
returnfalse;
}else{
$request="GET$queryHTTP/1.1rn";
$request.="Host:$url[host]rn";
$request.="Connection: Closern";
if($cookie)$request.="Cookie:$cookien";
$request.="rn";
fwrite($fp,$request);
while(!@feof($fp)){
$result.=@fgets($fp,1024);
}
fclose($fp);
return$result;
}
}
//獲取url的html部分,去掉header
functionGetUrlHTML($url,$cookie=false){
$rowdata=get_url($url,$cookie);
if($rowdata)
{
$body=stristr($rowdata,"rnrn");
$body=substr($body,4,strlen($body));
return$body;
}
returnfalse;
}
?>
示例代碼5:用fsockopen函數打開url,以POST方式獲取完整的數據,包括header和body
代碼如下:
<?
functionHTTP_Post($URL,$data,$cookie,$referrer=""){
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="")// if not given use this script. as referrer
$referrer="111";
// making string from $data
foreach($dataas$key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1n";
$request.="Host: ".$URL_Info["host"]."n";
$request.="Referer:$referern";
$request.="Content-type: application/x-www-form-urlencodedn";
$request.="Content-length: ".strlen($data_string)."n";
$request.="Connection: closen";
$request.="Cookie:$cookien";
$request.="n";
$request.=$data_string."n";
$fp=fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp,$request);
while(!feof($fp)){
$result.=fgets($fp,1024);
}
fclose($fp);
return$result;
}
printhr();
?>
示例代碼6:使用curl庫,使用curl庫之前,你可能需要查看一下php.ini,查看是否已經打開了curl擴展
代碼如下:
<?
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, '');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
關於curl庫:
curl官方網站
curl 是使用URL語法的傳送文件工具,支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL證書、HTTP POST、HTTP PUT 、FTP 上傳,kerberos、基於HTT格式的上傳、代理、cookie、用戶+口令證明、文件傳送恢復、http代理通道和大量其他有用的技巧
代碼如下:
<?
functionprintarr(array$arr)
{
echo"<br> Row field count: ".count($arr)."<br>";
foreach($arras$key=>$value)
{
echo"$key=$value <br>";
}
}
?>
⑷ php讀取文件內容判斷是否存在這個內容
一般使用curl獲取,以前可以用file_get_contents獲取
<?php
$res = file_get_contents('url地址或文件'); // 得到結果
if ($res != 'not') {
// 彈窗需要使用js
echo "<script>alert('輸出信息');window.location='需要跳轉的地址';</script>";
}
⑸ php 怎樣獲取文本內容中的圖片和文件路徑
$str='<imgsrc="http://localhost/2.jpg"alt=""/><imgsrc="http://localhost/2.jpg"alt=""/><imgsrc="http://localhost/2.jpg"alt=""/><ahref="http://www.,com/">aaa</a>';
$str=strip_tags($str,'<img>');
preg_match_all('/<imgs+src="([w:/.]+)"/',$str,$matches);
//var_mp($matches[1]);
$match=$matches[1];
foreach($matchas$value){
echo$value."<br>";
}
⑹ 關於Php 讀取php文件內容
不就是include嗎?
在b.php里,
include('a.php');
然後,就可以直接輸出a.php里的$ifopen變數了
⑺ 如何使用PHP讀取文本文件內容
利用PHP讀取文本文件的內容,其實很簡單,我們只需要掌握函數「file_get_contents();」的使用就可以了。下面,小編將作詳細的介紹。
工具/原料
電腦一台
WAMP開發環境
方法/步驟
file_get_content()函數介紹。使用file_get_contents()獲取txt文件的內容,具體參數說明如下:
2
具體實例說明。從文本文件tst.txt中讀取裡面的內容並顯示在瀏覽器中,具體代碼和圖示如下:
<?php
$file = 'tst.txt';
$content = file_get_contents($file); //讀取文件中的內容
echo $content;
?>
⑻ PHP讀取文件內容
把文本裡面的內容改成
<p style="color:red">you did not</p>
<p><strong> Your order could not be processed at this time.Please try again later.</strong></p>
<?php echo date('H:i,jS F Y'); ?>
試試