2013-07-19 78 views
0

在我的網站我有一個表與下面的代碼:PHP加載文本文件

<table width="100%" id="tabloadwar" cellspacing="1" cellpadding="0" border="0"> 
<?php 

$subdirs = array_map('intval', scandir("/war")); 
$max = max($subdirs); 

for($i=1;$i<=max;$i++){ ?> 
<tr> 
<td width="15%"><?php echo $i; ?></td> 
<td width="85%"> 
<?php $percorso = file("dir1/content.txt"); $line = file($percorso); echo $line?></td></tr> 

<?php } ?> 
</table> 

使用上面的代碼,我想創建一個具有儘可能多的行作爲目錄的計數表在我的服務器。順便說它不工作,因爲我沒有看到我的桌子內有任何東西。一排的一個例子是這樣的

<tr> 
<td width="15%">1</td> // i value 
<td width="85%">Italy</td> //text on the 1st line of my *.txt file 
</tr> 

而且,而不是name我必須把一個名爲dir1/content.txt文件的內容。它不工作,因爲我還沒有看到任何東西。任何幫助?

+0

'/ war'?也許你的意思是'/ var'?它是否存在? –

+0

不,這個文件夾被稱爲「戰爭」 –

+1

目前的輸出是什麼樣的?你有任何錯誤? – DevlshOne

回答

0

您必須繆斯$max代替max這裏for($i=1;$i<=max;$i++){

試試這個代碼,看看會發生什麼..

<?php 
    if (!file_exists("/war")) { 
    echo "'/war' not exists<br />"; 
    } 
    if (!file_exists("dir1/content.txt")) { 
    echo "'content.txt' not exists<br />"; 
    } 
?> 
<table width="100%" id="tabloadwar" cellspacing="1" cellpadding="0" border="0"> 
<?php 
    $subdirs = array_map("intval", scandir("/war")); 
    $max = max($subdirs); 

    for($i = 1; $i <= $max; $i++) { 
?> 
    <tr> 
    <td width="15%"><?php echo $i; ?></td> 
    <td width="85%"> 
    <?php 
     $lines = file("dir1/content.txt"); 
     echo implode("<br />", $lines); 
    ?> 
    </td> 
    </tr> 

<?php 
    } 
?> 
</table> 

你說....rows as the count of directories in my server...
,如果你想在文件夾子文件的數你可以使用globGLOB_ONLYDIR國旗

<?php 
    $dirs = glob("path/to/dir/*", GLOB_ONLYDIR); 
    $total_dirs = count($dirs); 
?>