⑴ php 如何讀取大文件里的內容
下個文件切割軟體,把50M切成每個3M左右(大小自己定義),然後就可以打開了。
⑵ 怎樣用PHP讀取一個word文檔內容並在瀏覽器中顯示出來
目前程序編譯語言有很多種,其中php是最為常見的一種編程語言。php讀取word文檔是很多朋友都想了解的,下面就由達內的老師為大家介紹一下。
?php
/*
*
必須將
php.ini
中的
com.allow_dcom
設為
TRUE
*/
function
php_Word($wordname,$htmlname,$content)
{
//獲取鏈接地址
$url
=
$_SERVER['HTTP_HOST'];
$url
=
";
$url
=
$url.$_SERVER['PHP_SELF'];
$url
=
dirname($url)."/";
//建立一個指向新COM組件的索引
$word
=
new
COM("word.application")
or
die("Unable
to
instanciate
Word");
//顯示目前正在使用的Word的版本號
echo
"Loading
Word,
v.
{$word-
Version}";
//把它的可見性設置為0(假),如果要使它在最前端打開,使用1(真)
$word->Visible
=
1;
//---------------------------------讀取Word內容操作
START-----------------------------------------
//打開一個word文檔
$word->Documents->Open($url.$wordname);
//將filename.doc轉換為html格式,並保存為html文件
$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$htmlname,8);
//獲取htm文件內容並輸出到頁面
(文本的樣式不會丟失)
$content
=
file_get_contents($url.$htmlname);
echo
$content;
//獲取word文檔內容並輸出到頁面(文本的原樣式已丟失)
$content=
$word->ActiveDocument->content->Text;
echo
$content;
//關閉與COM組件之間的連接
$word->Documents->close(true);
$word->Quit();
$word
=
null;
unset($word);
//---------------------------------新建立Word文檔操作
START--------------------------------------
//建立一個空的word文檔
$word->Documents->Add();
//寫入內容到新建word
$word->Selection->TypeText("$content");
//保存新建的word文檔
$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$wordname);
//關閉與COM組件之間的連接
$word->Quit();
}
php_Word("tesw.doc","filename.html","寫入word的內容");
?>
⑶ PHP讀取目錄下所有文件內容並顯示
<?php
function printFile($filepath)
{
//substr(string,start,length)函數返回字元串的一部分;start規定在字元串的何處開始 ;length規定要返回的字元串長度。默認是直到字元串的結尾。
//strripos(string,find,start)查找 "php" 在字元串中最後一次出現的位置; find為規定要查找的字元;start可選。規定開始搜索的位置
//讀取文件後綴名
//$filetype = substr ( $filename, strripos ( $filename, "." ) + 1 );
//判斷是不是以txt結尾並且是文件
#if ($filetype == "txt" && is_file ( $filepath . "/" . $filename ))
if ( is_file ( $filepath))
{
$filename=iconv("gb2312","utf-8",$filepath);
echo $filename."內容如下:"."<br/>";
$fp = fopen ( $filepath, "r" );//打開文件
#while (! feof ( $f )) //一直輸出直到文件結尾
$i = 1;
while ($i < 10)
{
$line = fgets ( $fp );
echo $line."<br/>";
$i = $i +1;
}
fclose($fp);
}
}
(此處空一行)
function readFileRecursive($filepath)
{
if (is_dir ( $filepath )) //判斷是不是目錄
{
$dirhandle = opendir ( $filepath );//打開文件夾的句柄
if ($dirhandle)
{
//判斷是不是有子文件或者文件夾
while ( ($filename = readdir ( $dirhandle ))!= false )
{
if ($filename == "." or $filename == "..")
{
//echo "目錄為「.」或「..」"."<br/>";
continue;
}
//判斷是否為目錄,如果為目錄遞歸調用函數,否則直接讀取列印文件
if(is_dir ($filepath . "/" . $filename ))
{
readFileRecursive($filepath . "/" . $filename);
}
else
{
//列印文件
printFile($filepath . "/" . $filename);
echo "<br/>";
}
}
closedir ( $dirhandle );
}
}
else
{
printFile($filepath . "/" . $filename);
return;
}
}
(此處空一行)
header("content-type:text/html;charset=utf-8");
#echo "Hello World"."<br/>";
$filepath = "C:/phpStudy/PHPTutorial/WWW/test/results"; //想要讀取的目錄
readFileRecursive($filepath )
?>
php還可以讀取文件夾下所有圖片,方法如下
hostdir=dirname(__FILE__).'/data/upload/admin/20170517/'; //要讀取的文件夾
(此處空一行)
$url = '/data/upload/admin/20170517/'; //圖片所存在的目錄
(此處空一行)
$filesnames = scandir($hostdir); //得到所有的文件
(此處空一行)
// print_r($filesnames);exit;
//獲取也就是掃描文件夾內的文件及文件夾名存入數組 $filesnames
(此處空一行)
$www = 'http://www.***.com/'; //域名
(此處空一行)
foreach ($filesnames as $name) {
$aurl= "<img width='100' height='100' src='".$www.$url.$name."' alt = '".$name."'>"; //圖片
echo $aurl . "<br/>"; //輸出他
⑷ php如何高效的讀取大文件
1. 直接採用file函數來操作
由於 file函數是一次性將所有內容讀入內存,而PHP為了防止一些寫的比較糟糕的程序佔用太多的內存而導致系統內存不足,使伺服器出現宕機,所以默認情況下限制只能最大使用內存16M,這是通過php.ini里的 memory_limit = 16M 來進行設置,這個值如果設置-1,則內存使用量不受限制
2.直接調用Linux的 tail 命令來顯示最 後幾行
在Linux命令行下,可以直接使用 tail -n 10 access.log 很輕易的顯示日誌文件最後幾行,可以直接用PHP來調用tail命令
3. 直接使用PHP的 fseek 來進行文件操作
這種方式是最為普遍的方式,它不需要將文件的內容全部讀入內容,而是直接通過指針來操作,所以效率是相當高效的。在使用fseek來對文件進行操作時,也有多種不同的方法,效率可能也是略有差別的
⑸ 用php讀取txt內容
$file
=
"t.txt";//要讀的文本
$fp
=
@fopen($file,
'r');//以直讀(r)方式打開文件【注意,是r不是a,具體參考手冊fopen函數】
$content
=
@fread($fp,
filesize($file));//讀取全部(filesize($file))內容
fclose($fp);//關閉文件
$content
=
preg_replace('/[\n\r]/is',
'<br/>',
$content);//將換行符換成HTML標簽的換行
//你上例中的123456789會換成123<br/>456<br/>789
echo
$content;//輸出文件
⑹ php如何獲取文件內容
PHP 中的file_get_contents() 函數可以實現
file_get_contents() 函數把整個文件讀入一個字元串中。
和 file() 一樣,不同的是 file_get_contents() 把文件讀入一個字元串。
file_get_contents() 函數是用於將文件的內容讀入到一個字元串中的首選方法。如果操作系統支持,還會使用內存映射技術來增強性能。
例如:
<?php
echo file_get_contents("test.txt");
?>
⑺ PHP讀取文件內容
把文本裡面的內容改成
<p style="color:red">you did not</p>
<p><strong> Your order could not be processed at this time.Please try again later.</strong></p>
<?php echo date('H:i,jS F Y'); ?>
試試
⑻ PHP讀取文件內容,和修改
function add_to_sitemap($url){
$doc = new DOMDocument;
$doc->load('111.xml');
//讀取遍歷
$ps = $doc->documentElement->getElementsByTagName('url');
//創建
$node = $doc->createElement("url");
//添加在最後一個元素後面
$newnode = $ps->item(0)->parentNode->appendChild($node);
//創建url的屬性
$locnode = $doc->createElement("loc");
$locnode->appendChild($doc->createTextNode($url));
$lastmodnode = $doc->createElement("lastmod");
$lastmodnode->appendChild($doc->createTextNode(date("Y-m-d")));
$newnode->appendChild($locnode);
$newnode->appendChild($lastmodnode);
//保存
$doc->save("111.xml");
}
你說的是簡單的文件操作
把文件內容讀入變數$contents
$fp = fopen('111.xml','r');
$contents = fread ($fp, filesize ('111.xml'));
fclose($fp);
$content = "你要保存的內容";
$fp = fopen('111.xml','wb+');
fwrite($fp,$content);
fclose($fp);
⑼ 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("文件無法打開"); }}
你可以去後盾人平台看看,裡面的東西不錯
⑽ 如何在php中對文件進行讀寫操作
嗯,很簡單,不過這次不是用file_put_contents()函數了,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
// 假設你的文件b.php已經創建,並且有權操作
// 但還是加上許可權設定的語句,比較保險
chmod(dirname(__FILE__), 0777); // 以最高操作許可權操作當前目錄
// 打開b.php文件,這里採用的是a+,也可以用a,a+為可讀可寫,a為只寫,如果b.php不能存在則會創建它
$file = fopen('b.php', 'a+'); // a模式就是一種追加模式,如果是w模式則會刪除之前的內容再添加
// 獲取需要寫入的內容
$c = '我是要被追加的內容!';
// 寫入追加的內容
fwrite($c, $file);
// 關閉b.php文件
fclose($file);
// 銷毀文件資源句柄變數
unset($file);