filesize(): stat failed for specific path - php

眉间皱痕 提交于 2021-02-18 20:12:05

问题


i am coding a simple doc managing script and need to get the file size and file type /file or folder/ in a table. somehow it doesn't work into the mention directory. please help if possible:

    <?php
$path = "./documents";
$dh = dir($path);
while( ($file=$dh->read()) ) 
{
    if( $file=="." || $file=="..")continue;
    echo "<tr><td><a href='download.php?f=$file' title='Click to Open/Download'>$file</a></td>";
    echo "<td>";
    echo (is_file($file))? "<img src='file.jpg'/> FILE" : "<img src='folder.jpg'/> FOLDER ";
    echo "</td><td>" .filesize($file)."</td>";
    echo "<td><input type='checkbox' name='delete[]'/></td></tr>";
}
?>

it does actually has 2 errors - one the file size doesn't work for the location, if i change it to path to "." - everything is ok, but if i try to change to the folder where i need it /documents ...all goes bad, and secondly - it doesn't take the right icon file as well, same type of problem. thank you


回答1:


Problem is, $file is only the filename without the directory prefix, so checking on it won't work. One way would be to have a variable with the absolute filename (say $realfile). You'd then have to alter your code and use this variable for the file checks:

<?php
$path = "./documents";
$dh = dir($path);
while(($file=$dh->read()) !== false) {
    if( $file=="." || $file=="..") continue;
    // have a new variable for the real filepath
    $realfile = $path . "/" . $file;
    echo "<tr><td><a href='download.php?f=$file' title='Click to Open/Download'>$file</a></td>";
    echo "<td>";
    echo (is_file($realfile))? "<img src='file.jpg'/> FILE" : "<img src='folder.jpg'/> FOLDER ";
    echo "</td><td>" .filesize($realfile)."</td>";
    echo "<td><input type='checkbox' name='delete[]'/></td></tr>";
}
?>


来源:https://stackoverflow.com/questions/34481697/filesize-stat-failed-for-specific-path-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!