zoukankan      html  css  js  c++  java
  • 文件把玩操作

    file() 将文件作为一个数组返回。数组中的每个单元都是文件中相应的一行,包括换行符在内。

    按换行回车来分段print_r(file("body.txt"));  用Ep替换文件 ----------为空就可以

        print_r(file("test.txt")); 用数组输出

    $body  = file_get_contents("url.txt");
    $bodyfile=explode(',',$body);用隔分 然后再数组输出
    print_r($bodyfile);

    for ( $v=0;$v < mt_rand( 1, 2);++$v)
       {
      $tempContent="<p>".$data[rarray_rand( $data )]."</p> ";
       $content=str_replace( $tempContent,"", $content );//替换掉重复数据
         $content=$content.$tempContent; }

    function rarray_rand( $arr )
    {
     return mt_rand( 0, count( $arr ) - 1 );
    }

    ========================================================

    file_get_contents() 跟file()一样,不同的是把文件读入一个字符串它的性能比 fread() 好得多。  vs file_put_contents()  echo file_put_contents("test.txt","Hello World!");

        echo file_get_contents("test.txt");

      file_put_contents(file,data,FILE_APPEND);  //比fwrite要好

    返回值

    该函数将返回写入到文件内数据的字节数。

    提示:使用 FILE_APPEND 可避免删除文件中已有的内容。

    ==========================================================

    fopen(filename,mode) r(文件头) w(覆盖文件头) a(追加文件尾)

    fread() 函数读取文件(可安全用于二进制文件)。 有长度         vs fwrite()

      语法

        fread(file,length)

      读取整个文件:

      <?php
      $file = fopen("test.txt","r");
      fread($file,filesize("test.txt"));
      fclose($file);
      ?>
    -------------------配合fopen()使用----------------------
    fopen(filename,mode)
    w写入方式打开,将文件指针指向文件头(头部插入)并将文件大小截为零。如果文件不存在则尝试创建之。
    a写入方式打开,将文件指针指向文件末尾(追加方式)。如果文件不存在则尝试创建之。

    fopen() 函数打开文件或者 URL。

    $file = fopen("test.txt","r");
    $file = fopen("/home/test/test.txt","r");
    $file = fopen("/home/test/test.gif","wb");
    $file = fopen("http://www.example.com/","r");
    $file = fopen("ftp://user:password@example.com/test.txt","w");

    如果打开失败,本函数返回 FALSE。

    ===============================================================

    $MyArray=array("a&f-osterreich-576.php","a&f-osterreich-630.php");

    foreach($MyArray as $key=>$value){
       $new = $value.".php";
      $fp=fopen("tmp.php","r");
        $str = fread($fp,filesize("tmp.php"));
        fclose($fp);
       $new = fopen($new,"w");
        fwrite($new,$str);
        fclose($new);          };

    <?php
    $a=array("Dog","Cat","Horse");
    array_pop($a);
    print_r($a);
    ?>

    输出:

    Array ( [0] => Dog [1] => Cat )
    file_put_contents($file,serialize($array));
    ====================================================(金典)读取目录==============================================================
    function getDir($dir) {
       
    $dirArray[]=NULL;
      
      if (false != ($handle = opendir ( $dir ))) {
          
        $i=0;
        
        while ( false !== ($file = readdir ( $handle )) ) {

                //去掉""."、".."以及带".xxx"后缀的文件
        
         //原来方式    if ($file != "." && $file != ".."&& !strpos($file,".txt")) {

        if (strpos($file,".php")) {

                    $dirArray[$i]=$file;

                    $i++;

                }

            }

            closedir ( $handle );

        }
     
       return $dirArray;

    }
    if (isset($_GET['u'])){;
    $files = getDir("./");
    foreach($files as $f)
    ================================================================================================================
    ===========================检测文件恢复==================================================
    ================================================================================================================

    function file_mode_info($file_path)
    {
    /* 如果不存在,则不可读、不可写、不可改 */
    if (!file_exists($file_path))
    {
    return false;
    }
    function writeXmlFile($xmlData)
    {
    $time = time(); //获取时间戳,用于给文件命名
    $path = dirname(__FILE__); //获取当前绝对路径
    $path = substr_replace($path, "", stripos($path, "actionsdata")); //将此文件所在的固有路径替换成空
    $path .= "xmlFiles"; //存放目录名
    /*判断目标目录是否存在,不存在则新建*/
    if(!is_dir($path))
    {
    mkdir($path); //新建目录
    }
    /*记录完整路径和文件名*/
    $filePathAndName = $path.$time.".xml";
    /*打开文件,文件名为<时间戳> + <.xml>*/
    $fp = fopen($filePathAndName, "w");
    if(!$fp)
    {
    return false;
    }
    /*写入文件流*/
    $flag = fwrite($fp, $xmlData);
    if(!$flag)
    {
    return false;
    }
    fclose($fp);
    return $filePathAndName;
    }

    ===================================url 获取文件名!=============================================

    ================================================================================

    代码如下:
    //获得当前的脚本网址
    function GetCurUrl()
    {
    if(!empty($_SERVER["REQUEST_URI"]))
    {
    $scriptName = $_SERVER["REQUEST_URI"];
    $nowurl = $scriptName;
    }
    else
    {
    $scriptName = $_SERVER["PHP_SELF"];
    if(empty($_SERVER["QUERY_STRING"]))
    {
    $nowurl = $scriptName;
    }
    else
    {
    $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
    }
    }
    return $nowurl;
    }


    方法一:
    复制代码 代码如下:
    <?php
    $url=$HTTP_SERVER_VARS['REQUEST_URI'];
    echo(str_replace('/','',$url));
    ?>

    方法二: (我的最爱)
    复制代码 代码如下:
    $url = $_SERVER['PHP_SELF'];
    echo $url;
    $filename= substr( $url,strrpos($url , '/')+1 );
    echo $filename;

    方法三:
    复制代码 代码如下:
    <?php
    $url = $_SERVER['PHP_SELF'];
    $arr = explode( '/' , $url );
    $filename= $arr[count($arr)-1];
    echo $filename;
    ?>

    方法四:
    复制代码 代码如下:
    <?php
    $url = $_SERVER['PHP_SELF'];
    $filename = end(explode('/',$url));
    echo $filename;
    ?>

  • 相关阅读:
    (critical) chassis-frontend.c:122: Failed to get log directory, please set by --log-path
    Zabbix MySQL percona 模板部署
    shell编程
    pt-online-schema-change
    Haproxy + Keepalived +PXC 常见错误
    c/c++获取系统时间函数
    《C++ Concurrency in Action》
    C++多线程学习资料参考
    C++11多线程教学
    软件学习网站
  • 原文地址:https://www.cnblogs.com/alex-13/p/3438224.html
Copyright © 2011-2022 走看看