首页 > 编程技术 > php

php stream_context_create函数

发布时间:2016-11-25 16:49

stream_context_create创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

函数原型:resource stream_context_create ([ array $options [, array $params ]] )

看个实例

//定义options数组
$opts=array
(
  'http'=>array
  (
    'method'=>"get",
    'header'=>"accept-language: enrn"."cookie: foo=barrn"
  )
);
//创建数据流上下文
$context=stream_context_create($opts);
/*向指定地址发送http请求
请求中包含附加的头部信息*/
$fp=fopen('http://www.111cn.net','r',false,$context);
//输出文件指针处的所有数据
fpassthru($fp);
//关闭文件
fclose($fp);
/*
该代码的输出结果为:即请求的cookie值
array
(
    [foo] => bar
)
*/

实例二

$default_opts=array
(
  'http'=>array
  (
    'method'=>"get",
    'header'=>"accept-language: enrn"."cookie: foo=bar",
    'proxy'=>"tcp://10.54.1.39:8000"
  )
);
$alternate_opts=array
(
  'http'=>array
  (
    'method'=>"post",
    'header'=>"content-type: application/x-www-form-urlencodedrn"."content-length: " . strlen("baz=bomb"),
    'content'=>"baz=bomb"
  )
);
$default=stream_context_get_default($default_opts);
$alternate=stream_context_create($alternate_opts);
/* sends a regular get request to proxy server at 10.54.1.39
* for www.example.com using context options specified in $default_opts
*/
readfile('http://www.example.com');
/* sends a post request directly to www.example.com
* using context options specified in $alternate_opts
*/
readfile('http://www.example.com', false, $alternate);

代码扩充获取远程主机socket信息*/
function getpeername()
{
  $remote_ip=$remote_port=0;         //定义ip
  socket_getpeername($this->fd,$remote_ip,$remote_port);   //返回主机信息
  return array($remote_ip,$remote_port);       //返回一个数组
}

?>

<?php教程
if(false==($socket=@socket_create(af_inet,sock_stream,sol_tcp)))  //如果创建socket失败
{
  die("couldn't create socket, error code is:".socket_last_error().",error message is: " . socket_strerror(socket_last_error()));          //输出错误信息与错误代码
}
?>

<?php
/*以下代码扩充获取本地主机socket信息*/
function getsockname()
{
  $remote_ip=$remote_port=0;         //定义ip
  socket_getsockname($this->fd,$remote_ip,$remote_port);   //返回主机信息
  return array($remote_ip,$remote_port);       //返回一个数组
}
?>


<?php
/*定义欲读取的数组*/
$read=array($socket1,$socket2);
/*执行操作*/
$num_changed_sockets=socket_select($read,$write=null,$except=null,0);
if($num_changed_sockets===false)
{
  /*error handling*/
}
else if($num_changed_sockets>0)
{
  /*at least at one of the sockets something interesting happened*/
}

opendir() 函数打开一个目录句柄,可由 closedir(),readdir() 和 rewinddir() 使用。

 

语法
opendir(path,context)
*/
$d=dir("c:windows");         //打开目录,并赋值给变量
echo "handle:".$d->handle."n";       //输出目录的handel属性
echo "<p>";
echo "path:".$d->path."n";        //输出目录的path属性
echo "<p>";
while(false!==($entry=$d->read()))       //通过循环读取对象的方法
{
  echo $entry."n";
}
$d->close();           //关闭目录句柄


//


$dir="c:windows";          //定义目录
if(is_dir($dir))           //如果变量为目录
{
  if($dh=opendir($dir));         //如果成功打开目录
  {
    echo "目录".$dir."成功打开";
  }
}
echo "<br>";
closedir($dh);          //关闭打开的目录句柄
echo "目录".$dir."已经关闭!";


///

$dir=" c:windows ";          //定义目录变量
if(is_dir($dir))           //判断变量是否为目录
{
  if($dh=opendir($dir))         //判断目录是否成功打开
  {
    while(($file=readdir($dh))!==false)      //循环读取目录中的文件信息
    {
      echo "filename:$file,filetype:".filetype($dir.$file)."n";  //输出文件名及文件类型
    }
    closedir($dh);          //关闭目录句柄
  }
}

///

if($handle=opendir('web'))         //如果目录顺利打开
{
  while(false!==($file=readdir($handle)))      //循环读取目录中的条目
  {
    if($file!="."&&$file!="..")        //如果文件不为当前或上级目录
    {
      echo "$filen";          //输出文件名称
    }
  }
  closedir($handle);          //关闭目录句柄
}

//

if($handle=opendir('08'))         //如果目录顺利打开
{
  while(false!==($file=readdir($handle)))      //循环读取目录中的条目
  {
    if($file!="."&&$file!="..")        //如果文件不为当前或上级目录
    {
      echo "$filen";          //输出文件名称
    }
  }
  rewind($handle);          //倒回目录句柄
  if(readdir($handle))       //判断是否倒回成功
  {
    echo "倒回成功";
  }
  else
  {
    echo "倒回失败";
  }
  closedir($handle);          //关闭目录句柄
}

//如果要对目录按字母排序如一

$files1=scandir($dir);         //按字母升序列出内容到数组
$files2=scandir($dir,1);        //按字母降序列出内容到数组

本文章收藏了大量的php中文件操作函数如有文件打开,创建,删除,更变组,读取写文件,文件上传以及打开远程文件,把内容写入文件等实例。

$fp=fopen("test.txt","r");
//以只读方式打开文件,将文件指针指向文件头
$fp=fopen("test.txt","r+");
//以读写方式打开文件,将文件指针指向文件头
$fp=fopen("test.txt","w");
//写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建
$fp=fopen("test.txt","w+");
//以读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建
$fp=fopen("test.txt","a");
//以写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建
$fp=fopen("test.txt","a+");
//以读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建
$fp=fopen("test.txt","wb+");
//以写入方式打开二进制文件,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建
$fp=fopen("c: est est.txt","r");
//在windows平台上,要转义文件路径中的每个反斜线,或者用斜线
$fp=fopen("http://www.domain.com/","r");
//以只读方式打开web地址
$fp=fopen("ftp://username:password@domain.com/test.txt","r");
//与ftp服务器连接,以username和password登录服务器
/*
本实例代码无输出结果
*/


$fp=fopen("test.txt","r");          //打开文件
$msg=fread($fp,filesize("test.txt"));        //读取文件内容到变量
print "$msg";            //输出文件内容
fclose($fp);            //关闭文件

//

$fp=popen("test.txt","r");          //以只读方式打开文件
$fp=popen($_post['command'],'r');
$read=fread($fp,2096);          //读取内容
echo $read;            //输出内容
pclose($fp);            //关闭文件
//这里用popen执行命令,命令是以post方式提交的参数,并返回执行结果。需要注意的是如果未找到要执行的命令,会返回一个合法的资源。它允许访问shell返回的任何错误信息。在打开了安全模式时,只能执行在safe_mode_exec_dir之内的程序,不能在指向程序的路径中包含..成分,即不能在指定目下外执行命令。

$fp=popen($_post['command'],'r');        //打开进程文件
$read=fread($fp,2096);          //读取进程文件指针到变量
echo $read;            //输出内容
pclose($fp);            //关闭进程文件

//

$file="test.txt";        //定义文件
if(file_exists($file))       //判断文件是否存在
{
  echo "下面清除缓存";
}
echo "<p>";
clearstatcache();       //清除文件状态缓存
if(file_exists($file))       //判断文件是否存在
{
  die('清除完毕');
}

//

$filename="test.txt";     //定义文件
$user="admin";      //定义用户
chgrp($filename,$group);    //改变文件/test/testfile.txt的所有者为"admin"

//

chmod("/test/testfile.txt",0600);
//只有文件所有者拥有读写权限
chmod("/test/testfile.txt",0644);
//文件所有者拥有读写权限,其他用户拥有只读权限
chmod("/test/testfile.txt",0755);
//文件所有者拥有所有权限,其他用户拥有读和执行权限
chmod("/test/testfile.txt",0750);
//文件所有者拥有所有权限,文件所有者所在用户组拥有读和执行权限

//

$file="test.txt";         //定义文件
delete($file);         //删除文件
//在php编程时一般还是用unlink函数来删除文件。


//

//以下代码实现文件上传功能,首先用move_uploaded_file函数上传文件;如果失败就用copy函数上传文件,上传到指定目录并修改目录属性。
//使用此代码要有文件上传权限,还要定义$path上传路径。另外必须有上传内容。单独使用,无输出内容
if(function_exists('move_uploaded_file') && move_uploaded_file($attachment,$path))
{
  chmod($path,0666);       //改变文件访问模式
  $attachment=$path;
}
elseif(copy($attachment,$path))     //如果move_upload_file就用cope()
{
  chmod($path,0666);       //改变文件访问属性
  $attachment=$path;
}

$file="test.txt";        //定义文件
echo fileowner($file);       //获取文件所有者
/*

fileowner() 函数返回文件的所有者。

若成功,则返回文件所有的用户 id。若失败,则返回 false。用户 id 以数字格式返回。

语法
fileowner(filename)

输出结果类似于:
0
*/

//以下函数实现删除所有".jpg"图片文件
foreach(glob("*.jpg")as $filename)       //查找与模式"*.jpg"匹配的文件
{
  echo "$filename size ".filesize($filename)."n";    //输出文件信息
  unlink($filename);          //删除文件
}
/*
glob() 函数返回匹配指定模式的文件名或目录。

该函数返回一个包含有匹配文件 / 目录的数组。如果出错返回 false。


*/

//将test.txt的最后修改时间改为2007年4月15日19点5分10秒
touch("test.txt",mktime(19,5,10,4,15,2007));

$file="test.txt";        //定义文件
echo fileperms($file);       //获取文件权限

/*
fileperms() 函数返回文件或目录的权限。

若成功,则返回文件的访问权限。若失败,则返回 false。

语法
fileperms(filename)
*/
//
$oldname="test.txt";          //定义原文件
$newname="test.txt.bak";         //定义修改后的文件名
if(rename($oldname,$newname))       //进行改名操作并对结果判断
{
  echo "改名成功!";         //输出相应信息
}
else
{
  echo "改名失败!";
}
/*
rename() 函数重命名文件或目录。

若成功,则该函数返回 true。若失败,则返回 false。

语法
rename(oldname,newname,context)

注释:在 php 4.3.3 之前,rename() 不能在基于 *nix 的系统中跨磁盘分区重命名文件。

注释:用于 oldname 中的封装协议必须和用于 newname 中的相匹配。

注释:对 context 的支持是 php 5.0.0 添加的。
*/

标签:[!--infotagslink--]

您可能感兴趣的文章: