开发者

Why isn't PHP filesize() function working?

I noticed that filesize doesn't work when I'm trying to list the contents of a directory using a path like this:

../

for example this works:

if ($handle = opendir('./')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {

            echo "File Name: $file | File Size: ". filesize($file)."<br />";            

        }
    }
    closedir($handle);
}

but this doesn't:

if ($handle = opendir('../')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {

            echo "File Name: $file | File Size: ". filesize($file)."<br />";            

        }
    }
    closedir($handle);
}

In the last example the file names are listed fine but instead of getting their file size I get this error:

Warning: filesize() [function.filesize]: stat failed for dynamic.php in C:\xampp\htdocs\app_file_manager\panel\index.php on line 65
开发者_如何转开发

Is there a way to get the 2nd example to work?


filesize($file) is failing because the $file is the name of the file that exists in the parent directory and not in the current directory.

You need to prefix $file with ../


Also, problem might be in:

  • If file corrupted ([function.filesize]: stat failed error most offen occurs with file uploaders)
  • In linux 32-bit this error will be for files that are larger 2GB


Why don't you define a variable like $basepath or $downloads folder path and append the filename to that variable and use filesize(). Be sure to avoid ./ and ../.

$downloads = "www/base/downloads"; 
$filepath = $downloads."/".$filename;
filesize($filepath)

This should work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜