, Guest!
Already a Member? Login or Register.

Menu



Showcase


Due to the volume of spam happening on our forums, posting is now restricted to verified members only.  If you're not verified, drop us a note with your username.

Home > FlexCMS Support Forum > User Help > Code Snippets > Automagically Generate Clickable File Links

FlexCMS Support Forum


Automagically Generate Clickable File Links
Started May 24, 2010 @ 12:14am by Wolf
Post Message 
Wolf




Posts: 65
 
Automagically Generate Clickable File LinksMay 24, 2010 @ 12:14am
I have a client who wanted to upload newsletters in PDF format so that clients could then click on a link to download them.

The catch is we didn't want the client to have to maintain hard coded links to the newsletters, so I came up with this little ditty...

Using File Manager, I created a directory structure called "/newsletters/pdf".

Then set permissions on the directory so that only the newsletter guy could upload to it via the new File Manager.

I created the following code snippet, then included the snippet in a block:


Quote



$query = "SELECT `Location` , `Filename` FROM `core-Files` WHERE `Location` = \"/newsletters/pdf/\" ORDER BY Filename asc";

$result = mysql_query($query) or die (mysql_error());

print '# Newsletters: ' . number_format(mysql_num_rows($result),0) . '<br><br>';

while ($row = mysql_fetch_array($result))
{ print '<a href="'.$BaseURL.$row['Location'].$row['Filename'].'" target="_blank">'.$row['Filename'].'</a><br>';
}



Now all the newsletter editor has to do is upload/delete/rename files as he chooses and the links are generated dynamically.

Cheers,
Wolf

PS - Be careful about naming your files... it's probably NOT a good idea to include spaces or special characters.
 

Last Edit: May 24, 2010 @ 2:56pm by Wolf
DCSun
Administrator



Posts: 625
 
May 24, 2010 @ 10:58am
Hey Wolf,

Neat script, thanks for sharing. One small change I would suggest is adding $BaseURL to the beginning of your link URL in the HREF argument of the link (ie: $BaseURL.$row['Location'].$row['Filename']). That way you'll always get the absolute link you intended, rather than relying on the browser to come up with the correct relative one (there may be issues with some browsers on deeper pages on the site because it may mistake "/pages/something" as folders and add your download link onto the end of that).


Dave


FlexCMS v3.2 Has Been Released!
 
Wolf




Posts: 65
 
Thanks and a question...May 24, 2010 @ 12:04pm
Dave,

Thanks!

I'd assumed that since File Manager created the info it would contain the necessary full path info. Is that not always the case?

Is this what you suggest?


Quote


$query = "SELECT `Location` , `Filename` FROM `core-Files` WHERE `Location` = \"/newsletters/pdf/\" ORDER BY Filename asc";

$result = mysql_query($query) or die (mysql_error());

print '# Newsletters: ' . number_format(mysql_num_rows($result),0) . '<br><br>';

while ($row = mysql_fetch_array($result))
{ print '<a href="'.$BaseURL.$row['Location'].$row['Filename'].'" target="_blank">'.$row['Filename'].'</a><br>';
}




Thanks!
Wolf
 

Last Edit: May 24, 2010 @ 2:54pm by Wolf
DCSun
Administrator



Posts: 625
 
May 24, 2010 @ 1:00pm
Need another period before the $ on BaseURL but yes that's where I meant to put it. The location in the files table is a relative path, such as /newsletters/, so depending on the URL you're at and how the browser interprets it you may end up with bad links. Adding the BaseURL variable (contains something like http://www.flexcms.com or http://somesite.com/installationfolder) in there will give you an absolute URL that will always be correct no matter what.

Dave


FlexCMS v3.2 Has Been Released!
 
Wolf




Posts: 65
 
::-)May 24, 2010 @ 2:55pm
Cool beans! Thanks Dave.

I corrected both examples to include your suggestion. Howdy

Wolf
 

Last Edit: May 24, 2010 @ 2:56pm by Wolf

Post Message 




Try & Buy FeedForAll - Easy to use RSS Feed Creator - great for iTunes users!

MEMBERS




All Contents, Code, Scripts and Technologies Copyright 2003-2009 FlexCMS.
All Rights Reserved. Software License Agreement

Processing Time: 0.09125 seconds.
 
Management Login

Powered By FlexCMS
Powered By FlexCMS