本文目录一览:
- 1、如何在一个文本文件中读取和写入php对象
- 2、读取某个PHP页面执行后的页面内容并返回怎么搞
- 3、怎么提取一个网站的PHP源码?
- 4、PHP怎么读取php所在文件夹下的图片和mp3文件,并且显示出来
- 5、怎么用php读取并显示另一个php文件的内容?
- 6、php如何读取文本指定的内容?
如何在一个文本文件中读取和写入php对象
file() 函数把整个文件读入一个数组中。
语法:file(path,include_path,context)
与 file_get_contents() 类似,不同的是 file() 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。
如果失败,则返回 false。
file_get_contents() 函数把整个文件读入一个字符串中。
语法:file_get_contents(path,include_path,context,start,max_length)
和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。
file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。
读取某个PHP页面执行后的页面内容并返回怎么搞
读取某个PHP页面执行后的页面内容并返回怎么搞
可以通过PHP中的require()、include()包含引用函数来实现返回主页面
例如:
require('1.php');
require('2.php');
或者:
include('1.php');
include('2.php');
require()、include()
怎么提取一个网站的PHP源码?
您好。一个网站的源代码是无法直接获取的。
您可以寻找一下该网站的页脚或者IE的顶部,是否存在类似 powered by xxx 这里的XX就是该PHP源码系统。然后百度搜索一下就可以找到了。
如果不存在类似的信息,那就不能通过正规途径获取了。
除非是利用某种不良途径获取。那就是寻找改网站的漏洞,通过入侵手段获取。不过这是违法的,不建议此种方式。
PHP怎么读取php所在文件夹下的图片和mp3文件,并且显示出来
?php
$dir = "./"; //要获取的目录
echo "********** 获取目录下所有文件和文件夹 ***********hr/";
//先判断指定的路径是不是一个文件夹
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh))!= false){
if( getFileType($file) == "mp3" ){
echo "mp3格式";
}
if( getFileType($file) == "jpg" || getFileType($file) == "png" || getFileType($file) == "gif"){
echo "图片格式";
}
closedir($dh);
}
}
function getFileType($filename) {
return strtolower(pathinfo($filename)['extension']);
}
?
怎么用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如何读取文本指定的内容?
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("rn","br /",$str);
}
?
--------第二种方法------------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//将整个文件内容读入到一个字符串中
$str = str_replace("rn","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("rn","br /",$str);
echo $str;
}
?
-------第四种方法--------------
?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$icount($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("rn","br /",$str);
echo $str;
}
?