Zach Starnes
Zach Starnes

Reputation: 3198

filemtime() and filesize() php error

I am trying to read all of the contents of a certain directory. I have the function below that loops through the directory and arranges all of the contents. The only problem is that I keep getting the filemtime(): stat failed and filesize(): stat failed for every file and directory. I have tried looking up the solution but none of what I have found has worked. I thought it might be a permission issue but I am able to read and write from the directory I am arranging so that isn't the problem. Does anyone know how I can fix this?

    public function arrangeContents()
    {
        $currDir = opendir($this->path);

        while($entryName = readdir($currDir))
        {
          $this->content[] = $entryName;
        }
        closedir($currDir);

        $this->content_count = count($this->content);

        for($i = 0; $i < $this->getContentCount(); $i++)
        {
            $this->names[] = $this->content[$i];
            $this->name_hrefs[] = $this->content[$i];

            $this->extns[] = $this->findExts($this->content[$i]); 

            $this->file_sizes[] = number_format(filesize($this->content[$i]));

            $this->mod_times[] = date("M j Y g:i A", filemtime($this->content[$i]));
            $this->time_keys[] = date("YmdHis", filemtime($this->content[$i]));

            $this->sortContents($this->content[$i]);
        }
    }

Upvotes: 0

Views: 384

Answers (2)

Zach Starnes
Zach Starnes

Reputation: 3198

I fixed this issue because I was not preceding the file with the path.

If I just put $this->dir . '/' . $this->content[i] within each of the functions it gave me everything correctly.

Upvotes: 0

David Lin
David Lin

Reputation: 13353

You're getting the file path wrong or you don't have permission to stat the relevant file.

  1. For path, try using a DIR constant or $_SERVER['DOCUMENT_ROOT'] to work out the full path

  2. For permission, the file be must be readable by the user under which PHP. try testing a folder of files with 777 just to prove permission is the issue.

Upvotes: 1

Related Questions