導航:首頁 > 編程語言 > 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讀取大文件內容相關的資料

熱點內容
方舟手游如何修改伺服器後台 瀏覽:815
程序員考試教程 瀏覽:219
文件名修改為文件夾的名字批處理 瀏覽:251
拍照程序員 瀏覽:827
wps怎麼把pdf轉jpg 瀏覽:217
自拍用什麼app做的藝術照 瀏覽:169
h3c無線配置命令 瀏覽:515
linux代碼閱讀工具 瀏覽:160
能夠畫出對稱圖形的是什麼app 瀏覽:424
單片機投票器 瀏覽:467
程序員那麼可愛唱嗎 瀏覽:830
手機誤刪的app怎麼恢復 瀏覽:700
java第三方加密庫 瀏覽:660
編譯代碼軟體哪個好 瀏覽:997
編譯器軟體圖片 瀏覽:880
美團專送app怎麼不接受遠單 瀏覽:833
伺服器mgmt口如何連接電腦 瀏覽:798
做程序員至少要精通幾種 瀏覽:673
個人用雲伺服器價格對比 瀏覽:257
如何遠程刪除伺服器文件夾 瀏覽:779