• PHP保存数组到文件中的方法


    ThinkPHP自3.1以后的版本,F函数保存数组时先序列化后再保存到文件中,因为我需要使用C方法来读取自定义配置文件,故需要把PHP数组保存到文件中以便C方法读取,PHP保存数组到文件的方法如下:
    $siteConfig = array('siteName' => '小策一喋 - 专注WEB开发及系统运维技术','keyword' => '前端设计,后端开发,系统运维');
    $content = '<?php return ' . var_export($siteConfig, true) . ';';
    file_put_contents( APP_PATH . "Common/Conf/config_site.php", $content);

     一行解决写入日志:

    file_put_contents("/tmp/jack.txt", var_export($layReturnArr,TRUE),FILE_APPEND);  

    一行完成Curl的POST输出:

    echo "curl -d"".http_build_query($data)."" ".$url;  

    临时调试:

    $result= var_export ( $_POST,True);  
    file_put_contents("/tmp/jack.txt",$result,FILE_APPEND);  

    路径变量:

    $uploadSliceLogName = "/data/htdocs/xiyou_devel/data/logs/".date("Ymd")."/uploadedSliceReceiveInfo.txt";                                               
    file_put_contents($uploadSliceLogName, var_export($_REQUEST,TRUE),FILE_APPEND);  
    $file_string = var_export ( $_POST,True);  
    file_put_contents("/tmp/var_export.txt",$file_string,FILE_APPEND);  

    在调试上传文件时,如苹果Android等

    @file_put_contents("log.txt",var_export($_GET,$_POST,$_FILES,file_get_contents("php://input"),true),FILE_APPEND); 

    ios下。如果图片过大。或者。。。有些异常情况发生的时候。

    function cache_write($file, $string, $type = 'array')  
    {  
         if(is_array($string))  
         {  
             $type = strtolower($type);  
             if($type == 'array')  
             {  
                 $string = "<?php
     return ".var_export($string,TRUE).";
    ?>";  
             }  
             elseif($type == 'constant')  
             {  
                 $data='';  
                 foreach($string as $key => $value) $data .= "define('".strtoupper($key)."','".  
    addslashes($value)."');
    ";  
                 $string = "<?php
    ".$data."
    ?>";  
             }  
         }  
         $strlen = file_put_contents(PHPCMS_CACHEDIR.$file, $string,FILE_APPEND);  //最好加上FILE_APPEND,否则tail -f出现
    tail: /tmp/friendviews.txt: file truncated   参看:http://www.xiangdong.org/blog/post/1614/
           chmod(PHPCMS_CACHEDIR.$file, 0777);  
           return $strlen;  
    } 
  • 相关阅读:
    【长篇高能】ReactiveCocoa 和 MVVM 入门
    圆形头像
    C#开发学习——.net C#中页面之间传值传参的方法以及内置对象
    C#开发学习——内联表达式
    C#开发学习——ADO.NET几个重要对象
    Android开发学习——动画
    Android开发学习—— Fragment
    Android开发学习—— ContentProvider内容提供者
    Android开发学习—— Service 服务
    Android开发学习—— Broadcast广播接收者
  • 原文地址:https://www.cnblogs.com/yhdsir/p/5026244.html
Copyright © 2020-2023  润新知