Page 1 of 1

Auto create links to files

Posted: Wed Jul 11, 2007 8:00 am
by David_Geoffrey
Hi

I would like to be able to send a weekly newsletter (word or pdf) via ftp to a folder  - which is then automatically picked up by CMS so that it is displayed in a list on the front end so that users can download it.

Is this possible?

Cheers

Re: Auto create links to files

Posted: Wed Jul 11, 2007 9:36 am
by cyberman
Have you tried to use Uploads module?

It can do what you want and has a download mode too ...

Re: Auto create links to files

Posted: Wed Jul 11, 2007 9:38 am
by scooper
Sure is. There's an awful lot you can do with a smidgen of programming knowledge and user defined tags.

For example, a simple tag to list all the files in a directory could be something like this:

Create a UDT and call it 'listFiles' (or whatever)

Code: Select all


	//pick a directory to look in.
	$directory=$config[root_path].'uploads/images/';
	//create a handler to use on that directory.
	$handler = opendir($directory);
	// and a string to add the code to.
	$stringToWrite='<ul>';

        // loop through until all files in directory have been read
        while ($file = readdir($handler)) {
       
		//if it's a file and not this directory or the parent then add it to the list.
		 if ($file != '.' && $file != '..')
	            $stringToWrite.='<li><a href="' . $directory . $file .'">' .  $file . '</a></li>';
	}

	// tidy up: close the handler
	closedir($handler);

	$stringToWrite.='</ul>';
	// and write out the string
	echo $stringToWrite;

Then just add that tag to your page using {listFiles}

Obviously that code above will need tweaking to your taste but it's a start.

.... or do what cyberman suggests. Not only is he a faster typer than me he tends to be right more often...

Re: Auto create links to files

Posted: Wed Jul 11, 2007 10:06 am
by cyberman
@scooper

Thanks for this little, but useful tag :) - maybe you can post it here

http://wiki.cmsmadesimple.org/index.php ... _tags_here

Re: Auto create links to files

Posted: Wed Jul 11, 2007 10:40 am
by scooper
Good suggestion - but looks like I've been beaten to it again.

Elijah has already submitted a tag that does pretty much exactly the same thing using the same method:

http://wiki.cmsmadesimple.org/index.php/Share_your_tags_here#listpictures

I'm just too darn slow this morning.

Re: Auto create links to files

Posted: Wed Jul 11, 2007 10:57 am
by David_Geoffrey
Hi All

Thank you very much for all your replies - I didn't realise that the uploads module did what I wanted. I shall try all your suggestions and pick the one that suits best

Thanks Again
DG

Re: Auto create links to files

Posted: Fri Aug 03, 2007 4:24 pm
by David_Geoffrey
This is not a CMS question but the fact is don't even have a smidgen of progamming knowledge as suggested by Scooper, so I am appealing to any programmers out there..

His wonderful tag works beautifully - except I want to sort the list by date, most recent at the top of the list. How do I do this? Can I do this?

Cheers
DG

Re: Auto create links to files

Posted: Fri Aug 03, 2007 8:41 pm
by Nullig
Something like this:

Code: Select all

$path = $config[root_path].'uploads/images/';

$t = 0;
$f = 0;
$files_arr['name'] = array();
$files_arr['time'] = array();

if (@$handle = opendir($path)) {
   while (false!== ($file = readdir($handle))) {
      if($file!= "." && $file!= "..") {
            $files_arr['time'][$t++] = filemtime($path.$file);
            $files_arr['name'][$f++] = $file;
         };
      };
      closedir($handle);
      arsort( $files_arr['time'] );
   }

echo '<ul>';

echo $files_arr['name'];
foreach ($files_arr['time'] as $key=>$ftime){
   $fname = $files_arr['name'][$key];
   echo '<li><a href="' . $path . $fname .'">' .  $fname . '</a></li>';
}

echo '</ul>';
Nullig

Re: Auto create links to files

Posted: Fri Aug 03, 2007 9:32 pm
by Nullig
Here's a tag that I use for listing files for download...

Suppose your site sells widgets and has a combination of pdf, doc and xls files for your customers to download. You could put these files in a directory called:

uploads/docs/widgets

which would contain files like this:

uploads/docs/widgets/Widget_No_1.pdf
uploads/docs/widgets/Widget_No_2.pdf
uploads/docs/widgets/Widget_No_3.pdf
uploads/docs/widgets/Widget_No_4.pdf
uploads/docs/widgets/Widget_Uses.doc
uploads/docs/widgets/Widget_Info.doc
uploads/docs/widgets/Widget_Comparison_Chart.xls

Create a tag called listDocs containing:

Code: Select all

$directory=$config[root_path] . 'uploads/docs/' . $params["docdir"] . '/';
$handler = opendir($directory);
$stringToWrite='<p>';
while ($file = readdir($handler)) {
   if ($file != '.' && $file != '..') {
      $extension = substr($file, -3, 3);
      $newfile = str_replace("_", " ", $file);
      $stringToWrite.='<a class="'. $extension . '" href="' . $directory . $file .'">' .  $newfile . '</a><br />';
   }
}
closedir($handler);
$stringToWrite.='</p>';
echo $stringToWrite;
add this to your stylesheet (you may need to create other entries for the types of files you have):

Code: Select all

#main .pdf{
  padding: 5px 0 5px 28px;
  background-image: url(uploads/images/pdf.gif);
  background-position: center left;
  background-repeat: no-repeat;
  color: #587A9C;
  text-decoration: none;
}

#main .doc{
  padding: 5px 0 5px 28px;
  background-image: url(uploads/images/doc.gif);
  background-position: center left;
  background-repeat: no-repeat;
  color: #587A9C;
  text-decoration: none;
}

#main .xls{
  padding: 5px 0 5px 28px;
  background-image: url(uploads/images/xls.gif);
  background-position: center left;
  background-repeat: no-repeat;
  color: #587A9C;
  text-decoration: none;
}
and call the tag like this:

Code: Select all

{listDocs docdir='widgets'}
This will display a list of linked files with a different graphic for each type of file and the "_" (underscores) in the filenames will be replaced with spaces, making them easier to read. This allows you to upload your docs into subfolders and call the tag for each folder on different pages.

Nullig