php读
连接到一个url地址为localhost、端口为3306的mysql服务器上。mysql服务器的帐号是"root",密码是"9999"。mysql服务器上有一个数据库ok,数据库里有一个表abc。表abc一共为两列,列名分别是"id"和"name",将abc里的所有数据读出来。
<?
$dbh=@mysql_connect("localhost:3306","root","9999");
/*定义变量dbh,mysql_connect()函数的意思是连接mysql数据库,"@"的意思是屏蔽报错*/
if(!$dbh){die("error");}
/*die()函数的意思是将括号里的字串送到浏览器并中断PHP程式(Script)。括号里的参数为欲送出的字串。*/
@mysql_select_db("ok",$dbh);
/*选择mysql服务器里的一个数据库,这里选的数据库名为ok*/
$q="SELECT*FROMabc";
/*定义变量q,"SELECT*FROMabc"是一个SQL语句,意思是读取表abc中的数据*/
?>
<br/>
<!--=========方法一=========-->
<br/>
<?
$rs=mysql_query($q,$dbh);
/*定义变量rs,函数mysql_query()的意思是:送出query字串供MySQL做相关的处理或者执行.由于php是从右往左执行的,所以,rs的值是服务器运行mysql_query()函数后返回的值*/
if(!$rs){die("Validresult!");}
echo"<table>";
echo"<tr><td>ID</td><td>Name</td></tr>";
while($row=mysql_fetch_row($rs))echo"<tr><td>$row[0]</td><td>$row[1]</td></tr>";
/*定义量变(数组)row,并利用while循环,把数据一一写出来.
函数mysql_fetch_row()的意思是:将查询结果$rs单列拆到阵列变数中.
$row[0]和$row[1]的位置可以换*/
echo"</table>";
?>
<br/>
<!--=========方法二=========-->
<br/>
<?
$rs=mysql_query($q,$dbh);
while($row=mysql_fetch_object($rs))echo"$row->id$row->name<br/>";
/*id和name可以换位置*/
?>
<br/>
<!--=========方法三=========-->
<br/>
<?
$rs=mysql_query($q,$dbh);
while($row=mysql_fetch_array($rs))echo"$row[id]$row[name]<br/>";
/*id和name可以换位置*/
?>
<!--=========方法三最快=========-->
<?
@mysql_close($dbh);
/*关闭到mysql数据库的连接*/
?>
② 怎么用php读取并显示另一个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怎么读
这么读 ,来 我教你:p h p
④ 用php读取txt内容
$file
=
"t.txt";//要读的文本
$fp
=
@fopen($file,
'r');//以直读(r)方式打开文件【注意,是r不是a,具体参考手册fopen函数】
$content
=
@fread($fp,
filesize($file));//读取全部(filesize($file))内容
fclose($fp);//关闭文件
$content
=
preg_replace('/[\n\r]/is',
'<br/>',
$content);//将换行符换成HTML标签的换行
//你上例中的123456789会换成123<br/>456<br/>789
echo
$content;//输出文件
⑤ 如何使用PHP读取文本文件内容
示例代码如下:
<?php
$file='test.txt';
$content=file_get_contents($file);//读取文件中的内容
echo$content;//输出显示
?>
需要提示一点的是:
文本文件的编码格式要与 php 的 charset 编码,以及 php 文件的字符编码,要求一致,否则可能会显示乱码。
⑥ 如何使用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如何动态读取一个文件内容
PHP只有反复的去读这个文件(可以读出来和上次内容进行比较),不能设置一个机关--让文件内容的变化的时候自动调用PHP其读文件。
⑧ php如何读取文本指定的内容
php读取文件内容:
-----第一种方法-----fread()--------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定读取大小,这里把整个文件内容读取出来
echo $str = str_replace("\r\n","<br />",$str);
}
?>
--------第二种方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-----第三种方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次读取 1024 字节
while(!feof($fp)){//循环读取,直至读取完整个文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-------第四种方法--------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行读取文件内容
echo $file_arr[$i]."<br />";
}
/*
foreach($file_arr as $value){
echo $value."<br />";
}*/
}
?>
----第五种方法--------------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行读取。如果fgets不写length参数,默认是读取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
⑨ PHP如何读出当前目录下所有文件
一般来说php中读取目录下的文件名的方式确实不少,最简单的是scandir,具体代码如下:x0dx0a复制代码 代码如下:$dir="./caxa/";x0dx0a$file=scandir($dir);x0dx0aprint_r($file);x0dx0a稍微复杂点的,来自于php手册:x0dx0a复制代码 代码如下:$dir = "/etc/php5/";x0dx0a// Open a known directory, and proceed to read its contentsx0dx0aif (is_dir($dir)) {x0dx0aif ($dh = opendir($dir)) {x0dx0awhile (($file = readdir($dh)) !== false) {x0dx0aecho "filename: $file : filetype: " . filetype($dir . $file) . "\n";x0dx0a} closedir($dh);x0dx0a}x0dx0a}x0dx0a这些都只能读取当前指定目录下的文件,对子目录中的文件则无法读取。原来自己写过一个循环删除所有目录的一段代码,需要逐个子目录删除所有文件,包括多层。但是只需要读出文件名,稍微复杂点,网上找到一个能用,原始代码有错误提示,改了一下引用&$data的地方,如下所示:x0dx0a复制代码 代码如下:function searchDir($path,&$data){x0dx0aif(is_dir($path)){x0dx0a$dp=dir($path);x0dx0awhile($file=$dp->read()){x0dx0aif($file!='.'&& $file!='..'){x0dx0asearchDir($path.'/'.$file,$data);x0dx0a}x0dx0a}x0dx0a$dp->close();x0dx0a}x0dx0aif(is_file($path)){x0dx0a$data[]=$path;x0dx0a}x0dx0a}x0dx0afunction getDir($dir){x0dx0a$data=array();x0dx0asearchDir($dir,$data);x0dx0areturn $data;x0dx0a}x0dx0aprint_r(getDir('.'));x0dx0a希望本文所述对大家的PHP程序设计有所帮助。