ZGD Posted May 20, 2016 Posted May 20, 2016 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?
kongondo Posted May 20, 2016 Posted May 20, 2016 (edited) filesizeStr...does that work? Just fishing here since I don't know why filesize shouldn't work but other properties do... Edited May 20, 2016 by kongondo
kongondo Posted May 20, 2016 Posted May 20, 2016 No errors? You have debug on? Some code we could have a look at?
adrian Posted May 20, 2016 Posted May 20, 2016 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?
ZGD Posted May 20, 2016 Author Posted May 20, 2016 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; }
adrian Posted May 20, 2016 Posted May 20, 2016 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. 1
matjazp Posted May 20, 2016 Posted May 20, 2016 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. 4
ZGD Posted May 23, 2016 Author Posted May 23, 2016 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.
Soma Posted May 23, 2016 Posted May 23, 2016 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. 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now