We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4594
    • 74 Posts
    Hello,

    my problem is:

    i have a list of links. But the links should not link to another document, but to files which the user can download by clicking on it (.jpg, .gif, .mp3, etc.).

    So my question is: Is there a way in modx for the client to
    1. upload downloadable files to a specific folder (e.g. assets/myfolder/folderwithdownloads/), then
    2. go to the editor, mark the text as link and diectly link to the file.?


    Currently i do it this way:
    1. I upload the downloadable files via ftp, then
    2. In the manager i create a weblink, target the weblink to the file.


    This is okay for me, but for my client it’s to difficult, i think. An if i have many files it is not very comfortable to create a weblink for each file....

    I’ve also tried the snippet filedownload, but it was not flexible enough for me...

    Thanks for help!

    Greets, captain
      • 3749
      • 24,544 Posts
      There’s probably an easier way but here’s what I did:

      I wanted users to be able to upload newsletters and to have a page of links to those newsletters.

      Here’s the snippet on the upload page (you have set up the document groups so that only members of the group can upload. If you want to allow anyone to upload, just leave out the &uploadgroups parameter).

      [[!FileUpload? &tpl=`MyNewsletterChunk`&path=`/assets/newsletters/`&uploadgroups=`wugNewsAdmins`&extensions=`doc,xls,pdf`!]
      


      Here’s the MyNewsletter Chunk:

      </p>
        <form enctype="multipart/form-data" action="[~[*id*]~]" method="post">
         <p>
          <input type="hidden" name="formid" value="[+fileupload.formid+]" />
          <!-- MAX_FILE_SIZE must precede the file input field -->
          <input type="hidden" name="MAX_FILE_SIZE" value="[+fileupload.maxsize+]" />
          Select file 1:<input name="userfile[]" type="file" /><br />
          Select file 2:<input name="userfile[]" type="file" /><br /> 
          <input type="submit" value="Upload file" />
         </p>
        </form>
       <p>


      To automaticall display the links, I wrote a snippet that reads the directory with the files, and outputs them as links. This code is probably more complicated than you need because it sorts them by filename. The filename has a particular format and contains the year and issue number so I had to sort them by issue number within each year.

      The Show Newsletter page just contains this snippet which includes the shownewsletters code:
      // Path where ShowNewsletters is installed
      define(SHOW_NEWSLETTERS_PATH, "assets/snippets/shownewsletters/");
      //
      //include snippet file
      include($modx->config['base_path'].SHOW_NEWSLETTERS_PATH.'shownewsletters.php');
      


      Note that the code below could simply replace the code in the snippet above. Having the php code in a separate file just makes it easier to edit and debug. Looking it over, I see that it could definitely use some informative else statements that report errors.

      This is what’s in shownewsletters.php:
      <?php
      
      // Shows all files in the newsletter directory,
      // descending by year,
      // assending by issue number within each year.
      
      // Filenames must be in format: yyyy-nn-MonthName
      // e.g. 2007-02-February
      
      $newline = "<br/>";
      $dir = $modx->config['base_path'].'assets/newsletters'; // set path to newsletter files
      $dir_array = array(); // main array - contains all file names in directory
      $temp_array = array(); // temp array used to hold one year's file names
      
      // open directory and parse file list 
      if (is_dir($dir)) { 
         if ($dh = opendir($dir)) { 
         
            // iterate over file list to create full directory array
            
            while (($filename = readdir($dh)) !== false) { 
                if (($filename != ".") && ($filename != "..") && ($filename != "WS_FTP.LOG")) { // skip self, parent, and ftp log
      
      		  	$dir_array[] = $filename; // add the filename to the array
      
                } 
            } 
      
            closedir($dh);  // close directory 
         }
         natsort($dir_array); // sort in ascending order by year and issue number
         $dir_array = array_reverse($dir_array, false); // reverse array (descending by year)
            
         $year = ""; // used for comparison to find new year
         $i = 0;
         $n = count($dir_array); // total number of files
         
         for($i=0;$i<$n;$i++) {  // can't use foreach() here -- need to manipulate index
      	    
         		$file_year = substr($dir_array[$i],0,4);  // year is first four characters of file name
      		
      		if ($file_year != $year) {  // new year
      			echo "<h3>    ",$file_year,"</h3>";
      			
      			$j = $i;  // set j (temporary index) to beginning of the new year
      			
      			$temp_array=array(); // empty $temp_array
      			$year = $file_year;  // set $year to current year
      			while($year == substr($dir_array[$j],0,4)) {  // build temp array for one year
      			
      			    if ($j > $n) {  // don't go beyond end of full array
      					break;
      				}
      				//$value = $dir_array[$j];
      				//$temp_array[] = $value;
      				$temp_array[] = $dir_array[$j];  // add one issue to temp_array
      				
      				$j++;
      			}
      			
      			
      			$temp_array = array_reverse($temp_array, false); // reverse array to show ascending months
      			
      			foreach($temp_array as $temp_value) {  // create one year's links on screen
      			
      			    $link = $modx->config['base_url'].'assets/newsletters/'.$temp_value;
              
      				echo '        <a href ="';
      	         	echo $link;
            			echo '">';
      				echo $temp_value;
      				echo '</a>';
                   	echo $newline;
      			}
      			$j--;  // decrement j because we went one too far
      			$i = $j;  // set  main index to beginning of next year
      			$year = $file_year;  // set $file year for start of new year
      		}
        
       
      	}
       
      } 
      
      
      ?>


      Hope this helps,

      Bob
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36624
        • 535 Posts
        hi,
        my english is bad so maybe i have misunderstood the question but i do that for simple links to files :

        tips 1:

        1) insert a link with tinyMCE in a richtextarea.
        2) in the popup window you can browse/upload files
        3) select one and it’s done..

        tips 2:

        1) create TV (file type)





          CTRL+SHIFT+U - Clear Cache
          CTRL+SHIFT+H - Hiding Heft Panel
          CTRL+SHIFT+N - Fast Create Resource
          CTRL+ALT+P - Preview Recource (in edit resorce window)
          CTRL+ALT+S - Save
          • 4594
          • 74 Posts
          thanks for answeing!

          I’ll look at it soon! I’ll post my solution here.

          Greetings, captain
            • 4594
            • 74 Posts
            @ manu

            i also thought it would be that easy. But that does not work for me.
            If i try to upload an image-file in the popup window to the "Files"-directory, a warning occurs "Error, Disallowed File Type". And i also can’t browse to another directory....
              • 36624
              • 535 Posts
              you can allow different files type for this file folder in the manager configuration (last tab).

              by default you can upload : txt,php,html,htm,xml,js,css,cache,zip,gz,rar,z,tgz,tar,htaccess,mp3,mp4,aac,wav,au,wmv,avi,mpg,mpeg,pdf,doc,xls
                CTRL+SHIFT+U - Clear Cache
                CTRL+SHIFT+H - Hiding Heft Panel
                CTRL+SHIFT+N - Fast Create Resource
                CTRL+ALT+P - Preview Recource (in edit resorce window)
                CTRL+ALT+S - Save
                • 4594
                • 74 Posts
                THANKS so much!

                i knew it would be that easy!!

                Greeting, captain