文件大小():特定路径的统计失败 - php

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34481697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:01:42  来源:igfitidea点击:

filesize(): stat failed for specific path - php

phpfilesizestat

提问by Ivan M

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

它实际上有 2 个错误 - 一个文件大小不适用于该位置,如果我将其更改为路径“。” - 一切正常,但如果我尝试更改到我需要它的文件夹 /documents ...一切都会变坏,其次 - 它也没有采用正确的图标文件,同样类型的问题。谢谢你

回答by Jan

Problem is, $fileis 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:

问题是,$file只有没有目录前缀的文件名,所以检查它是行不通的。一种方法是使用具有绝对文件名的变量(例如$realfile)。然后,您必须更改代码并使用此变量进行文件检查:

<?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>";
}
?>