導航:首頁 > 編程語言 > php讀取大文件內容

php讀取大文件內容

發布時間:2023-09-08 00:26:16

1. php如何讀取CSV大文件並且將其導入資料庫示例

思路:

讀取csv文件,每讀取一行數據,就插入資料庫

示例

文件夾結構

/
file.csv//csv大文件,這里只模擬三行數據,不考慮運行效率(PS:csv文件格式很簡單,文件一般較小,解析很快,運行效率的瓶頸主要在寫入資料庫操作)
index.php//php文件

file.csv

singi,20
lily,19
daming,23

index.php

/**
*讀取csv文件,每讀取一行數據,就插入資料庫
*/

//獲取資料庫實例
$dsn='mysql:dbname=test;host=127.0.0.1';
$user='root';
$password='';
try{
$db=newPDO($dsn,$user,$password);
}catch(PDOException$e){
echo'Connectionfailed:'.$e->getMessage();
}

//讀取file.csv文件
if(($handle=fopen("file.csv","r"))!==FALSE){
while(($row=fgetcsv($handle,1000,","))!==FALSE){
//寫入資料庫
$sth=$db->prepare('insertintotestsetname=:name,age=:age');
$sth->bindParam(':name',$row[0],PDO::PARAM_STR,255);
$sth->bindParam(':age',$row[1],PDO::PARAM_INT);
$sth->execute();
}
fclose($handle);
}

數據表

CREATETABLE`test`(
`id`INT(10)UNSIGNEDNOTNULLAUTO_INCREMENT,
`name`VARCHAR(255)NULLDEFAULT''COLLATE'utf8mb4_bin',
`age`INT(10)NULLDEFAULT'0',
PRIMARYKEY(`id`)
)
COLLATE='utf8mb4_bin'
ENGINE=InnoDB;

運行結束後,資料庫中會插入csv中的三行數據

2. php 使用file_get_contents讀取大文件的方法

當我們遇到文本文件體積很大時,比如超過幾十M甚至幾百M幾G的大文件,用記事本或者其它編輯器打開往往不能成功,因為他們都需要把文件內容全部放到內存裡面,這時就會發生內存溢出而打開錯誤,遇到這種情況我們可以使用PHP的文件讀取函數file_get_contents()進行分段讀取。
函數說明
string
file_get_contents
(
string
$filename
[,
bool
$use_include_path
[,
resource
$context
[,
int
$offset
[,
int
$maxlen
]]]]
)

file()
一樣,只除了
file_get_contents()
把文件讀入一個字元串。將在參數
offset
所指定的位置開始讀取長度為
maxlen
的內容。如果失敗,file_get_contents()
將返回
FALSE。
file_get_contents()
函數是用來將文件的內容讀入到一個字元串中的首選方法。如果操作系統支持還會使用內存映射技術來增強性能。
應用:
復制代碼
代碼如下:
$str
=
$content=file_get_contents("2.sql",FALSE,NULL,1024*1024,1024);
echo
$str;
如果針對較小文件只是希望分段讀取並以此讀完可以使用fread()函數
復制代碼
代碼如下:
$fp=fopen('2.sql','r');
while
(!feof($fp)){
$str.=fread($fp,
filesize
($filename)/10);//每次讀出文件10分之1
//進行處理
}
echo
$str;

3. php如何讀取文本指定的內容

php讀取文件內容:
-----第一種方法-----fread()--------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = fread($fp,filesize($file_path));//指定讀取大小,這里把整個文件內容讀取出來
echo $str = str_replace("\r\n","<br />",$str);
}
?>

--------第二種方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$str = file_get_contents($file_path);//將整個文件內容讀入到一個字元串中
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-----第三種方法------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str = "";
$buffer = 1024;//每次讀取 1024 位元組
while(!feof($fp)){//循環讀取,直至讀取完整個文件
$str .= fread($fp,$buffer);
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>
-------第四種方法--------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$file_arr = file($file_path);
for($i=0;$i<count($file_arr);$i++){//逐行讀取文件內容
echo $file_arr[$i]."<br />";
}
/*
foreach($file_arr as $value){
echo $value."<br />";
}*/
}
?>
----第五種方法--------------------
<?php
$file_path = "test.txt";
if(file_exists($file_path)){
$fp = fopen($file_path,"r");
$str ="";
while(!feof($fp)){
$str .= fgets($fp);//逐行讀取。如果fgets不寫length參數,默認是讀取1k。
}
$str = str_replace("\r\n","<br />",$str);
echo $str;
}
?>

4. PHP如何實現讀取指定文件內的某些內容

這個文件,如果是用php 語法寫的,你可以用include();將此文件包含進來,
這樣的話,這里文件裡面$index="132233123";
你就可以調用$index變數了
如果你寫的只是一個文件話,建議你用以下方式進行判斷
這個下面是我寫的一個讀取文件的函數,
function Read_Url($file_url){
$str="";
$handle = @fopen($file_url, "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$str .= $buffer." ";
}
return $str;
fclose($handle);
}else{
Msg("文件無法打開");
}
}

5. php讀取txt內容

<form action='' method="post">
輸入字母:
<input type="text" name="cs">
<input type="submit">
</form>
<?php
//獲取post值
$cs = empty($_POST['cs']) ? "" : $_POST['cs'];
//post值為空直接返回
if($cs==""){return '';}
//打開diqu.txt文件資源
$file = fopen("diqu.txt", "r") or exit("未找到文件!");
//逐行查找post傳遞的字元
while(!feof($file))
{
//如果找到post傳遞的字元就返回該行的值
if($val = strstr(fgets($file),$cs)){echo str_replace($cs,'',$val);return "";}
}
//關閉文件
fclose($file);
?>

閱讀全文

與php讀取大文件內容相關的資料

熱點內容
游戲源碼搭建一條龍 瀏覽:192
宋金pdf 瀏覽:807
伺服器為什麼需要內存池 瀏覽:526
php與jquery開發實例 瀏覽:289
編程大世界故事漫畫 瀏覽:983
北漂程序員出車禍 瀏覽:914
亞馬遜為什麼用雲端伺服器 瀏覽:65
程序員審核職位 瀏覽:385
德龍空調壓縮機 瀏覽:780
紅旗app如何注冊新賬戶 瀏覽:360
慣導pdf 瀏覽:606
c程序員的平均工資 瀏覽:58
微小店源碼 瀏覽:801
編譯原理答題題庫 瀏覽:169
ubuntu編程入門 瀏覽:301
antbuild命令 瀏覽:771
怎麼訂閱伺服器 瀏覽:593
視頻專用加密器哪個好用 瀏覽:295
app無法使用網路哪裡設置 瀏覽:847
紅旗linux怎麼安裝 瀏覽:136