Jump to content

Accessing filesize property of $file in admin template


ZGD
 Share

Recommended Posts

I'm currently building a module that checks image filesizes and returns a list of those that match some specific criteria. The list needs to be accessible from a page the module installs under the admin Setup page.

However I seem unable to access $file->filesize within an admin template. I can access other properties, like $file->title and $file->page. I've used the same code to echo a list of filesizes from an array of files on the frontend and it works perfectly. Is this normal behaviour?

Link to comment
Share on other sites

Out of curiosity, does this work:

filesize($file->filename);

Hang on a second - you say that:

$file->title works - but this is not properties of files :)

What exactly is $file referencing?

Link to comment
Share on other sites

Sorry adrian, you're right, I don't know why I mentioned $file->title! $file returns the filename, I must have been thinking of that. Updated question with strikethrough. 

No errors, and filesize($file->filename) also returns nothing unfortunately. Code below from the module:
 

public function ___execute() {

    // this will be dynamic later
    $fieldname = "image";

    $old_name = "";
    $pagesWithImages = wire('pages')->find($fieldname.".count>0, sort=title");
    $allImages = array();

    foreach($pagesWithImages as $p) {
        $imagedata = $p->get($fieldname);
        if (is_array($imagedata)) {
            foreach($imagedata as $image) {
                $allImages[] = $image;
            }
        } else {
            $allImages[] = $imagedata;
        }
    }

    $out .= "<h3>".count($allImages)." images found.</h3>";
    if (count($allImages)) {
        $adminURL = $this->config->urls->admin;
        $table = $this->modules->get('MarkupAdminDataTable');
        $table->setEncodeEntities(false); 
        $table->headerRow(array(
            $this->_x('Page', 'list-table'), 
            $this->_x('Filename', 'list-table'), 
            $this->_x('Filesize', 'list-table')
            ));

        foreach ($allImages as $i) {
            $pg = $i->page;
            $pagelink = "<a target='_blank' href='".$adminURL."page/edit/?id={$pg->id}'>".$pg->title." (".$pg->id.")</a>";

            $filesize = filesize($i->filename);

            $table->row(array(
                $pagelink,
                $i,
                $filesize
            )); 
        }
        $out .= $table->render();
    }
    return $out;
}
Link to comment
Share on other sites

Definitely weird - if $i->filename is definitely returning the full server path to the file, and PHP's filesize() isn't working on it, then I am baffled :)

Apparently some hosts disable filesize(), but you mention that it works on the frontend, so that would rule out that possibility.

  • Like 1
Link to comment
Share on other sites

Check what is $i (use TracyDebugger). Does: 

$pagesWithImages = wire('pages')->find($fieldname.">0, sort=title");

make a difference (removed .count)?

You can also use:

$imagedata = $p->getUnformated($fieldname);

then you don't need is_array check.

  • Like 4
Link to comment
Share on other sites

Thanks matjazp and adrian. Looks like I was trying to access the filesize property of an array of files rather than the files themselves. All solved now, might post the module up somewhere when I get a chance.

Link to comment
Share on other sites

An File field configured as single (max 1), will by default be a single object on front-end (where outputformatting is on), BUT in the backend where no outputformatting is on the field will be an WireArray, regardless of if single or multiple.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...