PHP STREAM

  1 <?php
  2 
  3 ///////////通用方法
  4 /** php 发送流文件 
  5 * @param  String  $url  接收的路径 
  6 * @param  String  $file 要发送的文件 
  7 * @return boolean 
  8 */  
  9 function sendStreamFile($url, $file){  
 10     if(file_exists($file)){  
 11         $opts = array(  
 12                 'http' => array(  
 13                 'method' => 'POST',  
 14                 'header' => 'content-type:application/x-www-form-urlencoded',  
 15                 'content' => file_get_contents($file)  
 16             )  
 17         );  
 18         $context = stream_context_create($opts);  
 19         $response = file_get_contents($url, false, $context);  
 20         return $response;
 21     }else{  
 22         return false;  
 23     }  
 24 }  
 25 
 26 /** php 接收流文件
 27 * @param  String  $file 接收后保存的文件名
 28 * @return boolean
 29 */
 30 function receiveStreamFile($receiveFile){
 31     $streamData = isset($GLOBALS['HTTP_RAW_POST_DATA'])? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
 32     if(empty($streamData)){
 33         $streamData = file_get_contents('php://input');
 34     }
 35     if($streamData!=''){
 36         $ret = file_put_contents($receiveFile, $streamData, true);
 37     }else{
 38         $ret = false;
 39     }
 40     return $ret;
 41 }
 42 
 43 ////发送页调用方法
 44 ////$ret = sendStreamFile('http://localhost:8055/11/usave.php', '1.php');  
 45 ////var_dump($ret);    
 46 
 47 ////接收页调用方法
 48 ////$receiveFile = 'receive.txt';  
 49 ////$ret = receiveStreamFile($receiveFile);  
 50 ////echo json_encode(array('success'=>(bool)$ret));      
 51 
 52 
 53 ///////////文件调用
 54 ///////////文件调用
 55 
 56 ////语音上传  保存文件流,返回文件名
 57 public function rstremfile(){
 58     $orderid=time().".mp3";
 59     $filename  = __ROOT__."Uploads/muise/".$orderid.".mp3";
 60     $ret = receiveStreamFile($filename);
 61     if($ret){
 62         $data['status'] = "0";
 63         $data['filename'] = "http://tt.zj1987.com/".$filename;            
 64     }else{
 65         $data['status'] = "1";
 66         $data['filename'] = "null";                
 67     }
 68     echo $data['filename'];
 69     exit;
 70 }
 71 ////语音上传  发送文件流,返回文件名
 72 public function sstremfile(){
 73     $url = $this->siteurl."/admin/inter/rstremfile";
 74     $filename=__ROOT__."log/log20170807.txt";
 75     $ret = sendStreamFile($url, $filename);  
 76     echo $ret;
 77     //var_dump($ret);  
 78 }    
 79 
 80 ?>
 81 
 82 <?php
 83 ////删除指定日期之前的文件,86400为1天
 84 function trash($folder,$time=86400){
 85     $ext=array('php','htm','html'); //带有这些扩展名的文件不会被删除.
 86     $o=opendir($folder);
 87     while($file=readdir($o)){
 88         if($file !='.' && $file !='..' && !in_array(substr($file,strrpos($file,'.')+1),$ext)){
 89             $fullPath=$folder.'/'.$file;
 90             if(is_dir($fullPath)){
 91                 trash($fullPath);
 92                 @rmdir($fullPath);
 93             } else {
 94                 if(time()-filemtime($fullPath) > $time){
 95                     $ttt1=time();
 96                     $ttt2=filemtime($fullPath);
 97                     echo "<br/>".$ttt1;
 98                     echo "<br/>".date("Y-m-d H:i:s",$ttt1);
 99                     echo "<br/>".$ttt2;
100                     echo "<br/>".date("Y-m-d H:i:s",$ttt2);
101                     unlink($fullPath);
102                     echo "<br/>".$fullPath;
103                 }
104             }
105         }
106     }
107     closedir($o);
108 }
109 //////调用自定义函数
110 ////trash('./');//调用自定义函数
111 ?>

猜你喜欢

转载自www.cnblogs.com/thinkbig/p/11876648.html
今日推荐