当前位置:网站首页>php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載

php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載

2022-06-24 19:19:00 owenzhang24

 
 
 
 

 

 

背景
本文介紹php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載

 

php文件操作介紹
  • fstat函數:顯示文件的所有信息
  • 文件讀取: fread($fp,filesize($file_path));
  • 寫入文件:file_put_contents($file_path,$con,FILE_APPEND);
  • 文件操作的應用:可以操作ini文件。將服務器的配置寫在ini文件中,再對其進行操作。
  • 拷貝文件:copy("e:\2.txt","d:\1.txt")
  • 創建文件夾:mkdir($path,0777,true)

 

workerman介紹
Workerman是一款純PHP開發的開源高性能的PHP 應用容器。
Workerman不是重複造輪子,它不是一個MVC框架,而是一個更底層更通用的服務框架,你可以用它開發tcp代理、做遊戲服務器、郵件服務器、ftp服務器、甚至開發一個php版本的redis、php版本的數據庫、php版本的nginx、php版本的php-fpm等等。Workerman可以說是PHP領域的一次創新,讓開發者徹底擺脫了PHP只能做WEB的束縛。
實際上Workerman類似一個PHP版本的nginx,核心也是多進程+Epoll+非阻塞IO。Workerman每個進程能維持上萬並發連接。由於本身常駐內存,不依賴Apache、nginx、php-fpm這些容器,擁有超高的性能。同時支持TCP、UDP、UNIXSOCKET,支持長連接,支持Websocket、HTTP、WSS、HTTPS等通訊協議以及各種自定義協議。擁有定時器、异步socket客戶端、异步Redis、异步Http、异步消息隊列等眾多高性能組件。

 

讀取文件
 //讀取文件        header("Content-Type: text/html;charset=utf-8");    //設置字符編碼        $file        = base\_path() . "/public/index.m3u8";        $read        = fopen($file, 'r');        $contents    = fread($read, filesize($file));        $string1     = getBeforeString($contents, 'enc.key');        $string2     = getLaterString($contents, 'enc.key');        $newContents = $string1 . 'enc.key?token=123' . $string2;        fclose($read);
寫入文件
 //寫入文件、        $file   = base\_path() . "/public/test22.m3u8";        $handle = fopen($file, 'w');              //打開文件        fwrite($handle, $newContents);             //寫入內容        fclose($handle);                           //關閉文件        readfile($file);                           //讀取並輸出文件全部內容        return redirect(WEB\_URL . '/test22.m3u8');//調轉頁面
讀取oss文件,讀取文件每行內容
//讀取OSS文件        $file = OSS\_URL . $url1;        //文件內容加入數組        $fileContent = @file($file);        $newContent  = '';        //遍曆文件的每一行        foreach ($fileContent as $value) {            //文件該行是否存在這個字符,有這替換內容            if (strpos($value, 'api.qingsong.chaotuapp.com/') !== false) {                $value = str\_replace('app.com/', "q.com/ckey", $value);            }            //文件該行是否存在這個字符,有這拼接內容            if (strpos($value, '.ts') !== false) {                $value = $ossUrl . $value;            }            $newContent .= $value;        }
臨時文件寫入,生成臨時文件並瀏覽器下載
wokerman框架
//寫入文件        $fileName = "$token.m3u8";        // ./59ee8147cf3f42575bc91ff586d54837.m3u8        $file = "./$fileName";        //打開文件        $handle = fopen($file, 'w');        //寫入內容        fwrite($handle, $newContent);        //將文件轉字符串        $content = file\_get\_contents($file);        //删除臨時文件        unlink($file);        //輸出瀏覽器        $response = new Response();        return $response->withHeader('content-description', 'File Transfer')            ->withHeader('content-type', 'application/force-download')            ->withHeader('content-disposition', "attachment; filename={$fileName}")            ->withHeader('content-transfer-encoding', 'binary')            ->withHeader('pragma', 'public')            ->withBody($content);

 

Buy me a cup of coffee :)

覺得對你有幫助,就給我打賞吧,謝謝!

微信贊賞碼鏈接,點擊跳轉:

https://www.owenzhang.com/wechat_reward.png

 
原网站

版权声明
本文为[owenzhang24]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241558321295.html