Jump to content

Large number of orphaned image files on pages with no images


Kiwi Chris
 Share

Recommended Posts

Every week or so, I'm finding large amounts of space taken up with orphaned image files on some  pages that have an image field but no images, with the image file names starting with a base number then numbered sequentially. I the worst cases this can amount to several hundred images.

I run code similar to what @ryan posted in this thread: 

however after a few days the large number of files are all back again taking up considerable amounts of space. These are pages that no one other than myself has editing access for. 

The one thing the pages in question have in common is that they do have child pages that have images, and the orphaned files that are duplicated many times are an image that belongs to the first of the sub-pages of the page where the large number of copies are created.

I have this function:

/*
* If a page doesn't have a header image explicitly set, search for the first child page that has a header image, and use that instead.
*/
function getHeaderImage($page) {
		if ($page->headerImage) {
				$headerImage = $page->headerImage;
		} else {
				foreach ($page->children as $item) {
						if ($item->headerImage) {
								$headerImage = $item->headerImage;
								break;
						}
				}
		}
		return $headerImage;
}

And in the templates affected this:

$page->headerImage = getHeaderImage($page);

It appears that every time I assign an image this way, a new copy is getting made into the page's /site/assets/files/ folder even though I never save $page.

Normally with other page properties, if they're set, but never saved, then they don't persist, but I know images may be different as they correspond to actual files on disk.

Is this expected behaviour?

Link to comment
Share on other sites

Part of the process that happens when you set a Files or Images field value is the copying of the file to the page's folder in /site/assets/files/.

On 1/10/2021 at 5:50 PM, Kiwi Chris said:

And in the templates affected this:


$page->headerImage = getHeaderImage($page);

The solution is not to set the Pageimage to the field. Just use some variable name like...

$header_image = getHeaderImage($page);

...or if you want it stored on the Page object then invent some property name that isn't a field name, e.g.

$page->the_header_image = getHeaderImage($page);

 

  • Like 3
Link to comment
Share on other sites

40 minutes ago, Robin S said:

Part of the process that happens when you set a Files or Images field value is the copying of the file to the page's folder in /site/assets/files/.

The solution is not to set the Pageimage to the field. Just use some variable name like...


$header_image = getHeaderImage($page);

...or if you want it stored on the Page object then invent some property name that isn't a field name, e.g.


$page->the_header_image = getHeaderImage($page);

 

Thanks. I worked out the first approach which works as a quick fix, but it's good to know the second one will work as well, as I think from an OO programming perspective, it's clearer to use the second option as I am effectively setting a page property, just I'm doing it at runtime, and as a non-persisted property.

  • Like 1
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...