News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Can I make a portal to show the most current “document”?

Started by rcane, January 27, 2022, 09:41:48 AM

Previous topic - Next topic

rcane

Our forum members get blastmails each week (or thereabouts) in their email.

I have a topic that is a bbcode list of those PDFs (links to the actual pdf files uploaded to the server and not attachments).

That gets tedious.

Here's what I envisioned:

1. A folder on the server to dump the pdf(s)

2. A portal that has clickable image (or some clickable eye-catcher) to read it.

3. But, I'd like the "button" to grab the most recently added document so I don't have to keep renaming files or changing the link. 

Does that make sense?

Drop a new file in the folder and it becomes the target based on its submission date.

I'm not sure if this qualifies as a portal question but it seems like the most likely way to display this.


Kindred

this would require a custom coding...   I'm going to move this out of support and into coding.
Слова
Украина

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

rcane

Quote from: Kindred on January 27, 2022, 12:56:37 PMthis would require a custom coding...   I'm going to move this out of support and into coding.

ok

Doug Heffernan

Quote from: rcane on January 27, 2022, 09:41:48 AMOur forum members get blastmails each week (or thereabouts) in their email.

I have a topic that is a bbcode list of those PDFs (links to the actual pdf files uploaded to the server and not attachments).

That gets tedious.

Here's what I envisioned:

1. A folder on the server to dump the pdf(s)

2. A portal that has clickable image (or some clickable eye-catcher) to read it.

3. But, I'd like the "button" to grab the most recently added document so I don't have to keep renaming files or changing the link. 

Does that make sense?

Drop a new file in the folder and it becomes the target based on its submission date.

I'm not sure if this qualifies as a portal question but it seems like the most likely way to display this.


As per your title, yes it can be done, if I understood it correctly. However this will require some work imo. I think that it would be best if this was moved to the Mod Request section. It might receive more attention there.

Arantor

So you'll need to put this in a PHP page or a PHP block - there's no way to just do this in a portal page otherwise (at least not without a lot more faffing about)

<?php

// These should end in /
$real_path '/var/www/html/';
$url_path 'https://domain.com/path/to/pdfs/';
$files = [];

$real_path rtrim($real_path) . '/';

foreach (
scandir($real_path) as $file) {
if (is_dir($real_path $file)) {
continue;
}

$files[$file] = filemtime($real_path $file);
}

arsort($files);
$latest array_keys($files)[0];

echo 
'<a href="' $url_path $latest '"><img src="banner.jpg"></a>';

You will need to set $real_path to the path to the PDFs (the full path, e.g. /home/username/public_html) and not just whatever your FTP client reports (since most FTP clients present /home/username as if it is / when it really isn't), and you will need to set $url_path to the URL for the folder.

This isn't clever, it will just look at all the things in the folder and return the most recent as a link with an image in it - I don't know the URL to your eye-catching banner image, but that's in the code as banner.jpg, which you might need to change.
No good deed goes unpunished / All helpful urges should be circumvented

I have something to say: it's better to burn out than to fade away. There can be only one.

rcane

wow thanks.

So, understanding what's done here.  Why did you trim the $real_path?

that would help me understand the loop, which seems to exit the loop when it discovers a file--and then goes on to grab it's mod time and sort them in the array (descending order).

Is that close?

rcane

Quote from: Arantor on January 27, 2022, 02:10:02 PMSo you'll need to put this in a PHP page or a PHP block - there's no way to just do this in a portal page otherwise (at least not without a lot more faffing about)

<?php

// These should end in /
$real_path '/var/www/html/';
$url_path 'https://domain.com/path/to/pdfs/';
$files = [];

$real_path rtrim($real_path) . '/';

foreach (
scandir($real_path) as $file) {
if (is_dir($real_path $file)) {
continue;
}

$files[$file] = filemtime($real_path $file);
}

arsort($files);
$latest array_keys($files)[0];

echo 
'<a href="' $url_path $latest '"><img src="banner.jpg"></a>';

You will need to set $real_path to the path to the PDFs (the full path, e.g. /home/username/public_html) and not just whatever your FTP client reports (since most FTP clients present /home/username as if it is / when it really isn't), and you will need to set $url_path to the URL for the folder.

This isn't clever, it will just look at all the things in the folder and return the most recent as a link with an image in it - I don't know the URL to your eye-catching banner image, but that's in the code as banner.jpg, which you might need to change.

how would you call this from within the forums if it's in a php file?

@rjen

You would need to put it in a php block in the portal... which makes sense, since you specially mentioned portals..
Running SMF 2.1 with latest Tinyportal at www.fjr-club.nl
Testing SMF 2.1 with next version Tinyportal at test.fjr-club.nl

Arantor

Argh, I meant that to be rtrim($real_path, '/') so that whether there was a / on the end or not, that got normalised out, though Linux doesn't care *too* much about // in a path.

I explicitly aimed for this being a portal block rather than a file, because it would have to be done differently as a raw file.
No good deed goes unpunished / All helpful urges should be circumvented

I have something to say: it's better to burn out than to fade away. There can be only one.

rcane

Quote from: Arantor on January 28, 2022, 05:09:29 AMArgh, I meant that to be rtrim($real_path, '/') so that whether there was a / on the end or not, that got normalised out, though Linux doesn't care *too* much about // in a path.

I explicitly aimed for this being a portal block rather than a file, because it would have to be done differently as a raw file.

Ok, just checking.  I'm just taking a php course online and trying to convert Swift into php on the fly.  It's starting to make some sense.

I've never added a block to a portal so that should be fun too.  Simple or Tiny portals seems to be the popular  ones for 2.0

Oldiesmann

TinyPortal is the one I'd recommend. SimplePortal is no longer developed.
Michael Eshom
Christian Metal Fans

Advertisement: