获取远程文件内容保存,并获取文件相关信息;本地文件内容添加

Original 2019-03-10 11:22:01 223
abstract:/*  * 获取远程文件保存本地并获取相关信息  * @p $src  远程文件路径  * @p $localPath    本地保存文件路径  * @p $ext     &nb
/*
 * 获取远程文件保存本地并获取相关信息
 * @p $src  远程文件路径
 * @p $localPath    本地保存文件路径
 * @p $ext      本地保存文件后缀
 * return  array*/
function get_file_info($src,$localPath='test',$ext='.html'){
    $str = file_get_contents($src);
    $filePath = $localPath.$ext;
    if(!file_put_contents($filePath,$str)){
        return '文件获取失败';
    }
    $kong=array();
    $kong['fileType']=filetype($filePath);
    $kong['fileSize']=filesize($filePath);
    $kong['ctime']=date('Y-m-d H:i:s',filectime($filePath));
    $kong['mtime']=date('Y-m-d H:i:s',filemtime($filePath));
    $kong['atime']=date('Y-m-d H:i:s',fileatime($filePath));
    $kong['paths']=pathinfo($filePath);
    return $kong;
}
//$re = get_file_info('http://img.php.cn/upload/avatar/000/000/001/8dd18f1e1ae4907a738fd106ac717c87.png','img','.png');
//var_dump($re);
/*
 * 向文件添加内容
 * @p $src      源文件路径
 * @p $content  添加内容
 * @p $flag     是否覆盖
 * return  string
 * */
function file_add($src,$content,$flag=false){
    if(!file_exists($src)) return '文件不存在';
    if($content=='') return '添加内容不能为空';
    if(is_array($content)||is_object($content)) $content=json_encode($content);
    if(!$flag){
        if(file_put_contents($src,$content)) return'内容覆盖成功';
        return '内容覆盖失败';
    }else{
        $content = "\r\n".$content;
        if(file_put_contents($src,$content,FILE_APPEND)) return'内容添加成功';
        return '内容添加失败';
    }
}
echo file_add('text3.csv','ss',true);


Correcting teacher:韦小宝Correction time:2019-03-10 13:04:54
Teacher's summary:写的很不错 在以后的日常开发中有很多东西会用到这些相关的函数 一定要没事多去练习练习

Release Notes

Popular Entries