使用filetime时的php警告()

时间:2017-07-05 08:58:50

标签: php html-table

我在php中制作了一个获取目录中文件名的表。 像这样:

$myDirectory = opendir("./server/sandro/Sandro-PC/Printscreens/");
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}


closedir($myDirectory);


$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

sort($dirArray);
print("<TABLE class=table>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");

for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ 
        echo('<TR><TD><a href="server/sandro/Sandro-PC/Printscreens/'.$dirArray[$index].'"><img height="80" width="120" src="server/sandro/Sandro-PC/Printscreens/'.$dirArray[$index].'"</a></td>');
        print("<td>");
        echo(filemtime($dirArray[$index]));
        print("</td>");
        print("<td>");
        print(filesize($dirArray[$index]));
        print("</td>");
        print("</TR>\n");
    }
}
print("</TABLE>\n");

但是在函数filemtime()filesize()中 给出这个错误:

  

警告:第167行的C:\ xampp \ htdocs \ dashboard3 \ printscreens.php中的image.png的filemtime():stat失败

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在上面的代码中,它是获取文件名但没有获取文件路径。当我们执行filesize()或filemtime()时,请使用目录传递文件名。我只是修改下面的代码,请尝试

<?php

 $myDirectory = opendir("./server/sandro/Sandro-PC/Printscreens/");
 $mdir = "./server/sandro/Sandro-PC/Printscreens/";
while($entryName = readdir($myDirectory)) {
    $dirArray[] = $entryName;
}


closedir($myDirectory);


$indexCount = count($dirArray);
Print ("$indexCount files<br>\n");

sort($dirArray);
print("<TABLE class=table>\n");
print("<TR><TH>Filename</TH><th>Filetype</th><th>Filesize</th></TR>\n");

for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 1) != "."){ 
        echo('<TR><TD><a href="server/sandro/Sandro-PC/Printscreens/'.$dirArray[$index].'"><img height="80" width="120" src="server/sandro/Sandro-PC/Printscreens/'.$dirArray[$index].'"</a></td>');
        print("<td>");
        //echo(filemtime($dirArray[$index]));
        $filename = "$mdir\\$dirArray[$index]";
        echo $filename;
if (file_exists($filename)) {
    echo "$filename was last modified: " . date ("F d Y H:i:s.", filemtime($filename));
}
        //echo filemtime($dirArray[$index]);
        print("</td>");
        print("<td>");
       // print(filesize($dirArray[$index]));
        print("</td>");
        print("</TR>\n");
    }
}

print("</TABLE>\n");

?>