Jump to content

alternative image source | folder


ngrmm
 Share

Recommended Posts

i have jpegs inside a folder (images).  the folder is located in the templates-folder of PW
site > templates > images

every 30 minutes a script generates  and saves jpegs into the images-folder. the all have a unique filename.
is it possible to save all those images into a image-field through API?
if not, is it possible to add the newest one into the imagefield?

i found this

$p = $pages->get(1001);
$p->of(true); // if in a template code, turn off output formatting
$p->images->add("http://placehold.it/350x150.jpg");
$p->save();
$p->of(false);

but i don't know how to grab the url automaticly of the newest file.

Link to comment
Share on other sites

You don't need the URL, you also can use the filename, as the images are all in your webspace filesystem.

$imagedir = $config->paths->templates . "images/";

To grab the latest imagefile, you have to read and compare file timestamps, like last modified. Check PHP functions for that and directory iteration:

https://secure.php.net/manual/en/function.filemtime.php

https://secure.php.net/manual/en/function.dir.php

 

  • Like 2
Link to comment
Share on other sites

i was able to get it this was:

 

		$files = glob("../dir/*.*");
		$files = array_combine($files, array_map("filemtime", $files));
		arsort($files);
		$latest_file = key($files);

 

  • Like 1
Link to comment
Share on other sites

?

Not tested and not 100% sure, but you can remove one function:

$files = glob("../dir/*.*");
$files = array_combine($files, array_map("filemtime", $files));
$latest_file = array_pop($files);

 

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...