1. 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/>"; //輸出他
2. PHP 獲取網頁中用戶輸入的數據的函數
用戶在表格form
中填寫數據,然後提交到一個php文件,PHP文件使用函數獲取數據
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit" value="提交">
</form>用戶填寫完username後提交到welcome.php文件,在welcome.php文件中,
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>$_POST["name"]就是用戶輸入的名字
3. php中file_get_contents()函數用法實例
我們先來看一下php中的
file_get_contents()函數的語法
string
file_get_contents(string
$
filename,bool
$
include_path
=
false,resource
$
context,int
$
offset
=
0,int
$
maxlen)
filename是文件或URL的名稱。
include_path如果啟用,則在include_path中搜索文件
context這是用於修改流的行為的選項集
offset此值指定要讀取的文件的起始位置。
maxlen此值指定要讀取的位元組數。
將文件內容讀取為字元串
這個php示例將從文件中讀取內容並存儲到字元串變數中。
<?php
$
content
=
file_get_contents(「input.txt」);
echo
$
content;
?>
將內容從URL讀取到字元串
<?php
$content
=
file_get_contents("http://example.com");
echo
$content;
?>
以上就是關於php中file_get_contents()函數的相關知識點,感謝大家的閱讀和對腳本之家的支持。
您可能感興趣的文章:PHP
fopen()和
file_get_contents()應用與差異介紹
4. PHP 抓取網頁內容的幾個函數
獲取所有內容url保存到文件function get_index($save_file, $prefix="index_"){
$count = 68;
$i = 1; if (file_exists($save_file)) @unlink($save_file);
$fp = fopen($save_file, "a+") or die("Open ". $save_file ." failed"); while($i<$count){
$url = $prefix . $i .".htm"; echo "Get ". $url ."...";
$url_str = get_content_url(get_url($url)); echo " OKn"; fwrite($fp, $url_str);
++$i;
} fclose($fp);
}
5. php里有沒有指定讀取第幾行的函數
php提供了內置函數fgets(),從文件指針中讀取一行。代碼如下:<?php $file=fopen("D:\\CHENCHENG\\myqq.txt","r"); while(! feof($file)){ $rows = fgets($file); echo "你要的第三行內容就是$rows[2]";}
6. 如何使用PHP讀取文本文件內容
利用PHP讀取文本文件的內容,其實很簡單,我們只需要掌握函數「file_get_contents();」的使用就可以了。下面,小編將作詳細的介紹。
工具/原料
電腦一台
WAMP開發環境
方法/步驟
file_get_content()函數介紹。使用file_get_contents()獲取txt文件的內容,具體參數說明如下:
2
具體實例說明。從文本文件tst.txt中讀取裡面的內容並顯示在瀏覽器中,具體代碼和圖示如下:
<?php
$file = 'tst.txt';
$content = file_get_contents($file); //讀取文件中的內容
echo $content;
?>