We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18654
    • 191 Posts
    If I run this script outside of the Modx Framework it works fine:

    <?php	
    
    		$filepath = '/home2/mattcdav/public_html/modx_6102/assets/media/mysteryreligions.flv';
    		
    		// check that it exists and is readable
    		if (file_exists($filepath) && is_readable($filepath)) {
    
    			$size = filesize($filepath);
    			header('Content-Type: ' . $applicationType);
    			header('Content-Length: ' . $size);
    			header('Content-Disposition: inline; filename=Mystery Religions');
    			header('Content-Transfer-Encoding: binary');
    			// open the file in binary read-only mode
    			$file = fopen($filepath, 'rb');
    		}
    		
    		if ($file) {
    		  	// stream the file and exit the script when complete
    		  	fpassthru($file);
    		  	exit;
    		}	
    		
    ?>
    


    But when I run it inside the framework I get this error:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 268810241 bytes) in /home2/mattcdav/public_html/modx_6102/core/model/modx/modscript.class.php(89) : eval()’d code on line 24

    The file is a large video file (about 225 MB). Is there any way to download large files within the ModX framework, or must I redirect to a script outside of the framework for large file downloads?

    Also, if I do need to use and external file, is there a way that I can access session variables that are set in ModX outside of Modx - this way I can pass the file information without using a form or making it visible in the URL.
      God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
      • 1343 ☆ A M B ☆
      • 2,213 Posts
      Is there a reason why you don’t use a typical link? I would think <a href="assets/media/mysteryreligions.flv">Video</a> would work. If your trying to force flvs to download instead of opening you could use something like this in your htaccess: <files *.flv>ForceType applicaton/octet-stream</files>
        Patrick | Server Wrangler
        About Me: Website | Tweets |  MODX Hosting
        • 18654
        • 191 Posts
        Yes, there is a reason for not just using a link. The download is for members only - I don’t want people having free access to the URL. Only logged in paid members should be able to access the download, so I want to route it through php rather than have them go directly to the url so I can make sure that they have sufficient privileges to access the download.
          God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
          • 22303 MODX Staff
          • 10,725 Posts
          Use Static Resources, this is exactly what they are for. It treats large files differently this way already: they are streamed out via readfile() rather than trying to return the content to the output buffer before rendering to the browser.
            • 18654
            • 191 Posts
            I’ve actually been using Static Resources, but I’ve just been grabbing the content via a script and then using my own code for the download. I hadn’t though about actually going to the the Static Resource URL. I went ahead and tried this approach as you suggested, but I wasn’t successful. It tried to download an .htm file instead of the file referred to by my Static Resource. Here’s a short screencast illustrating my attempt.

            EDIT:
            Ok, I just realized that the content type probably has to match the download type, but there is not content type for an .flv file and I can’t find where this list is populated from. Also, why not just have the content type determined by the file extension automatically?

            If I am able to get the Static Resources working via a URL - how can I protect that URL so that only authorized users can access the download? I’m thinking that I would use a plugin for that, but I’m not sure what the most efficient method would be. Perhaps something like:

            if (page is a static resource) {
            Get resource groups associated with this static resource;
            Compare resource groups with user membership level
            if (ok) {
            keep going
            }
            else {
            Figure out referring page
            send back to this page with a header
            ]

            When a member is logged in, their access level(s) are saved in a SESSION variable. So, for every protected resource the access levels of the member have to be checked against the resource groups of the resource. So, if a Static Resource is in the resource groups church, member and individual, and the user is associated with the user group "individual" than access is allowed.
              God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
              • 22303 MODX Staff
              • 10,725 Posts
              You are making this way too complicated. All you need to do is create the Content Type record with the proper MIME type and the extension you want it to work with, and this will just work. Also, security is then the same as any MODx Resource using the Resource Group/User Group access controls; just make sure the location of the actual physical file is either outside the web root or in directory that is inaccessible directly to the browser.
                • 18654
                • 191 Posts
                Quote from: OpenGeek at Nov 23, 2009, 08:26 AM

                All you need to do is create the Content Type record with the proper MIME type and the extension you want it to work with...

                How do I do this - directly in the database? I didn’t see anywhere in system settings to add MIME types and extensions.
                  God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
                  • 28215
                  • 4,149 Posts
                  Quote from: mattcdavis1 at Nov 23, 2009, 01:37 PM

                  How do I do this - directly in the database? I didn’t see anywhere in system settings to add MIME types and extensions.

                  top menu: System -> Content Types.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 18654
                    • 191 Posts
                    Ahhh.... - Thanks! I was so stuck on looking in system settings I didn’t even notice that option on the dropdown men.
                      God does not save those who are only imaginary sinners. Be a sinner, and let your sins be strong, but let your trust in Christ be stronger, and rejoice in Christ who is the victor over sin, death, and the world.
                      • 25862
                      • 9 Posts
                      Hi!

                      I am newbie in Modx. I also need to protect the files from unregistered users.
                      A small question - if file is in inaccessible to the browser directory, how can I associate it with Modx resource? I need a file description to be stored somewhere. huh