0

我还是 PHP 新手,我正在尝试将文件夹中的文件回显到表格行。我可以成功地在服务器上回显文件,但我为文件类型尝试的所有内容都不起作用。到目前为止,我的代码如下所示:

<?PHP 

$folder = "uploads/"; 
$handle = opendir($folder); 


# Making an array containing the files in the current directory: 
while ($file = readdir($handle)) 
{ 
    $files[] = $file; 
} 
closedir($handle); 

#echo the files 
$path_parts = pathinfo($file);
foreach ($files as $file) { 
    echo "

    <tr><td><a href=$folder$file>$file</a></td><td>$path_parts['extension']</td><td>date</td>   </tr>"
    ; 
} 
?>

按照这种方式设置,它给了我一个服务器错误。当我取出路径部分/路径信息代码时,它工作正常,但当然它不会回显文件类型。对此的任何帮助将不胜感激,如果有人知道实现文件上传日期的最佳方法,那就太好了。顺便说一句,如果有任何区别,请使用 uploadifive 作为上传者。

4

2 回答 2

1

如前所述,$path_parts变量赋值需要在你的 foreach 循环内。为了节省一些代码行,您应该将scandir视为读取文件夹中文件的 while 循环的替代品。

以下是您的代码在进行这些更改后的外观:

<?PHP 

$folder = "uploads/"; 

# Making an array containing the files in the current directory: 
$files = scandir($folder); 

#echo the files 
foreach ($files as $file) { 
    $path_parts = pathinfo($folder.$file);
    echo "<tr><td><a href={$folder}{$file}>{$file}</a></td><td>{$path_parts['extension']}</td><td>date</td></tr>"; 
}
于 2012-11-18T23:52:02.353 回答
0

我对您的代码做了一些细微的修改:

<?php
$folder = "uploads/"; 
$handle = opendir($folder); 

# Making an array containing the files in the current directory: 
while ($file = readdir($handle)) 
{ 
    $files[] = $file; 
} 
closedir($handle); 

foreach ($files as $file) 
{
    #echo the files 
    $path_parts = pathinfo($file);
    echo "<tr><td><a href='" . $folder . $file . "'>" . 
         $file . "</a></td><td>" . $path_parts['extension'] . 
         "</td><td>date</td></tr>";
} 

大多数情况下,$path_parts = pathinfo($file);必须在循环本身中。否则,您仅在$file循环的最后一个执行路径信息。

于 2012-11-18T23:45:14.320 回答