當前位置:首頁 » 編程語言 » php文件讀取

php文件讀取

發布時間: 2022-01-14 13:59:47

A. php如何動態讀取一個文件內容

PHP只有反復的去讀這個文件(可以讀出來和上次內容進行比較),不能設置一個機關--讓文件內容的變化的時候自動調用PHP其讀文件。

B. 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'); ?>
試試

C. 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>";
}
}
?>

D. 關於PHP讀寫文件

相關
php函數

fopen()打開文件。
格式如:fopen("文件路徑","r")。
fopen()函數有參數第一個參數要指明文件,第二個參數可以是r,w等,讀文件時就可以是r,寫文件時可以是w。
fwrite()和
fputs()寫文件。
fclose()
關閉文件

fgets()讀取記錄。最常用的是以上這些函數。

E. PHP如何實現讀取指定文件內的某些內容

PHP的 fgets(從文件指針中讀指定內容)

語法:
fgets(filepointer)

filepointer,要讀取的文件指針。如果成功,從文件中讀取一行並返回字元串,如果失敗,返回 FALSE。

示例:

<?php
$fp=fopen("test.txt","r");
if($fp)
{
for($i=1;!feof($fp);$i++)
{
echo"行".$i.":".fgets($fp)."<br/>";
}
}
else
{
echo"打開文件失敗";
}
fclose($fp);
?>

假設test.txt的內容為:

hello world
hello cnblogs
hello heihaozi
hello everyone
頁面輸出的結果為:

行1 : hello world
行2 : hello cnblogs
行3 : hello heihaozi
行4 : hello everyone

F. php 讀取txt文件內容

  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. ?>


G. 如何使用PHP讀取文本文件內容

示例代碼如下:

<?php
$file='test.txt';
$content=file_get_contents($file);//讀取文件中的內容
echo$content;//輸出顯示
?>


需要提示一點的是:

文本文件的編碼格式要與 php 的 charset 編碼,以及 php 文件的字元編碼,要求一致,否則可能會顯示亂碼。

H. php 如何讀取本機文件

你的問題也就是PHP讀取客戶端的文件問題。

道理上來講,我認為是不可以的,因為PHP是運行在服務端的,而且也要保證客戶端的安全吧?

但可能可以解決的方法我認為也有兩種,你不妨試試!

一:

利用JS,但必須是IE瀏覽器內核!因為用到的是IE插件ActiveXObject

<!DOCTYPEhtml>
<htmllang="zh-CN"><head><metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">

<title>TestActiveXObject</title>
<script>
functionReadFiles(){
if(window.ActiveXObject){
//alert("yes");
varfso=newActiveXObject("Scripting.FileSystemObject");
varasd=fso.CreateTextFile("C:\a.txt");//創建文件
asd.WriteLine("56984566");
asd.close();
varkk=fso.OpenTextFile("c:\a.txt");//打開文件
while(!kk.atEndOfLine){
document.writeln(kk.readLine());//讀取文件,並輸出
}
}
else{
//alert("no");
}
}
</script></head>
<bodyonclick="ReadFiles();">ffffffffff
</body>
</html>

這是一段HTML里的JS,你可以參考!然後加以修改。

方法二:

是一個構思,不知道能否實現!

是做為客戶端的,讓程序自行上傳你想要的文件,然後達到可以看到的目前

I. PHP文件讀取問題

$myfile=fopen($filename);
少了一個必須的參數,讀取內容的大小。如果是讀取整個文件可以:
$myfile=fopen($filename,filesize($filename));
也可以讀取指定大小,後面的單位我忘了是kb還是位元組

J. php讀取逐行讀取文件

換個1mb的文本它肯定有空格換行,具體操作如下代碼:

$file = file("welcome.txt");
foreach($file as &$line) echo $line.'<br />';

這個更方便, file()直接把文本按行轉換成數組
fgets如果沒指定第二參數,將直接讀取到緩存結束為止, 其實它不以換行來循環的,它的第二參數也是限制每次讀取的字元個數而已。

熱點內容
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:88
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:502
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:651
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:477
電腦主伺服器怎麼開機 發布:2024-09-20 07:19:07 瀏覽:728
2022款瑞虎升級哪些配置 發布:2024-09-20 06:59:07 瀏覽:265
資料庫與asp 發布:2024-09-20 06:55:25 瀏覽:727
python解釋編譯 發布:2024-09-20 06:52:57 瀏覽:648
舞蹈豐收腳本 發布:2024-09-20 06:36:26 瀏覽:595
linux進程埠號 發布:2024-09-20 06:36:11 瀏覽:81