首页 > 编程技术 > php

php 给文件尾部增加内容

发布时间:2016-11-25 15:55

如果我们想添加到一个文件,我们需要以追加方式打开它。下面的代码了这一点。

If we want to add on to a file we need to open it up in append mode. The code below does just that.

*/

 代码如下 复制代码

$text = "test.txt";
if( file_exists( $text ) && is_file( $text ) )
{
 $fh = fopen($myFile, 'a');
 if( $fh )
 {
  $stringData = "New www.111cn.net 1 ";
  if( fwrite($fh, $stringData))
  {
   echo '增加'.$stringData.'成功';
  }
  else
  {
   die('文件不可写');
  }
  $stringData = "New 111cn.net 2 ";
  fwrite($fh, $stringData);
  fclose($fh);

 }
}

/*
本站原创教程转载注明来源于www.111cn.net
*/

//方法一 用while来些fgets一行行读
$file_name="1.txt";

 代码如下 复制代码

$fp=fopen($file_name,'r');

 while(!feof($fp))
 {
  $buffer=fgets($fp,4096);
  echo $buffer."<br>";
 }
 fclose($fp);


 
// 方法二 用file一次保存到数组再用foreach输出

 代码如下 复制代码
$array = file( $file );
foreach( $array as $v =>$_v )
{
  echo $_v,'<br />';
}


//方法三用file_get_contents一次读出

 代码如下 复制代码
if( is_file( $file_name ) )
{
 $cn = file_get_contents( $file_name );
 echo $cn;
}


/*
相关函数参考地址
fopen http://www.111cn.net/phper/18/d5da4dcc30303f7684a91dec184e24fc.htm
feof 是否到文件末
fgets http://www.111cn.net/phper/21/php-fgets.htm
fclose 关闭
file http://www.111cn.net/phper/24/f5d9e092cfa826b89c45b339bdd3dd4f.htm
file_get_contents http://www.111cn.net/phper/24/f8d52eaae81ea27383375ead36ffbd4d.htm
foreach http://www.111cn.net/phper/18/foreach-foreach.htm
*/

创建文件在php中会用到fopen来创建,会用的\\\"w\\\"或\\\"w+“来创建,所面有关于他的简要说明 fopen函数需要两个重要的信息块正常运行。首先,我们必须提供的文件,我们希望它来打开它命名。第二,我们必须告诉我们什么功能上与该文件做计划

*/

 代码如下 复制代码
$fn = 'www.111cn.net.txt'
$fcon ='php 如何创建一个文件'; //内容
if($f=fopen($fn,"w"))
 {
       if( fwrite($f, $fcon) )
    {
      echo '写文件成功';
    }
    else
    {
      echo '文件不可写';
    }
    fclose($f);
 }
 else
 {
  echo '不能创建文件,请检查权限';
 }
   


/*
$fn = 'www.111cn.net.txt';要创建文件名

"w" 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
"w+" 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
fclose($r) //关闭打开的文件。 fclose需要的文件句柄被关闭
本站原创教程转载注明来源www.111cn.net
*/

本程序可以显示WEB目录下的所有目录及后缀为php,php3,htm,html的文件名,并可以查看源文件,很适合作为编写php程序的工具。

第一个文件名为 phplist.inc

 代码如下 复制代码
<?
global $htmlroot;
$htmlroot="/web/html"; //"/web/html"是apache的文档根目录,用户可以根据自己的配置修改
?>

第二个文件名为 codeshow.php

 代码如下 复制代码
<?
highlight_file($filename); //高亮显示文件代码
?>

这个是主程序名为 phplist.php

 代码如下 复制代码
<?
require ("phplist.inc");
if ($rootpath=="") {$rootpath=$htmlroot;}
function filelist($pathname){ //遍历所有的文件和目录,$pathnam为起始目录,$searchs为搜索关键字
global $htmlroot;
if (eregi("[/][.][.]$",$pathname)) {
$temp=split("[/]",$pathname);
$pathname="";
for ($tt=1;$tt<(count($temp)-2);$tt++)
$pathname=$pathname."/".$temp[$tt];
}
echo $pathname."
";
$handle=opendir($pathname); //打开目录
if (@chdir($pathname)) { //cd 进入目录,若无权限则不进入
$file = readdir($handle); //读出所有当前目录下的元素名,第一次读出为‘。’,指上层目录
while ($file = readdir($handle)) { //若有元素就进行以下处理
$fname=$pathname."/".$file; //将元素名与当前目录名结合组成完整的文件名,并赋值给$fname
if ((is_file($file)) and (ereg(".php[3]{0,1}$",$file) or ereg(".htm[l]{0,1}$",$file))) //判断是否为文件并且是否以.php和.php3结尾
{
$temppp=split($htmlroot,$fname);
echo "<a href=$temppp[1]>".$file."</a>   ".filesize($file)."bytes      <a href=codeshow?filename=$fname><font size=-1>源文件</font></a>
";
//findinfile($fname,$searchs); //调用findinfile函数
}
elseif (is_dir($fname)) { //判断是否为目录
linkres($fname);
//filelist($fname,$searchs);} //递归调用filelist函数
}
}
chdir(".."); //结束当前递归,返回上层
}
closedir($handle); //关闭当前目录读取
}
//
function linkres($filename){
$tpath=split("/web/html",$filename);
$turepath=$tpath[1];
ereg("[^\/~]{0,}$",$filename,$res);
//$ft=filetitle($filename);
//if ($ft=="") $ft="无标题";
echo "<a href=phplist.php?rootpath=$filename>".$res[0]."</a>   <dir>
";
}
//
filelist($rootpath);

?>

 <?
/ / PHP的新闻抓取由Neil Moomey,。
/ /你可以自由的使用此代码作为您的愿望。
/ /请确保您可以从任何网站,你抓从标题许可。
/ /你可能需要写上您的服务器上的标题,文件以加快速度。

/ /抓斗从文件或网页的源代码网站
if(!($myFile=fopen(http://www.111cn.net,"r")))
{
echo "The news interface is down for maintenance.";
exit;
}
while(!feof($myFile))
{
// Read each line and add to $myLine
$myLine.=fgets($myFile,255);
}
fclose($myFile);
/ /解压缩一切的开始和结束之间。您需要包括这些行
/ /在标题或选择一些独特的子在HTML庆祝开始
/ /和新闻结束。


$start="<hr align=";
$end="建立镜像";
$start_position=strpos($myLine, $start);
$end_position=strpos($myLine, $end)+strlen($end);
$length=$end_position-$start_position;
$myLine=substr($myLine, $start_position, $length);
// Display HTML
echo $myLine;
?>

标签:[!--infotagslink--]

您可能感兴趣的文章: