PHP讀取txt文檔

讀取txt

$local_path = "files/url.txt";
        $ch = curl_init($file_path);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        file_put_contents($local_path, curl_exec($ch));
        curl_close($ch);
        //3、打開文件
        $fileInfo = fopen($local_path, "r");
        if (!$fileInfo) {
            return ['status' => 1, 'message' => '文件無法打不開'];
        }
        //4、計算url數量
        $dataInfo = $result = [];
        while (!feof($fileInfo)) {
            $redeem_code = trim(fgets($fileInfo));
            if (preg_match("/^[0-9a-zA-Z][0-9a-zA-Z\-]{3,31}$/", $redeem_code)) {
                $dataInfo[] = $redeem_code;
            }
        }
        fclose($fileInfo);
        if (empty($dataInfo)){
            return ['status' => 1, 'data' => "號碼包格式不正確"];
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章