We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22282
    • 10 Posts
    Server issue not snippit issue

    Hello,

    I have setup the snippet as posted by mediaguy as the original script doesn’t play well with large files.
    Unfortunately I am getting the following error:

    Fatal error: Class ’Download’ not found in /srv/www/htdocs/mysitename/manager/includes/document.parser.class.inc.php(770) : eval()’d code on line 6.

    I assume it is related to these lines (6&7) of the snippet.
    $alias = $_REQUEST['q'];
    if (!$alias) $alias = Download::GetAlias($_REQUEST['id']);


    I am running modx v. 1.0.3

    Any help would be greatly appreciated.
      • 22282
      • 10 Posts
      Please ignore the above issue, it was a problem with the configuration of my testing server. I didn’t dig into what due to time constraints but it works on my production server.

      I am still having a major issue however of all large files getting corrupted on download. All the files that I will be serving are over 50MB so this is a major issue for me.

      Unlike the standard FiledownloadPE the file is the correct size just broken.

      Any ideas??

      Thanks.
        • 22282
        • 10 Posts
        Quote from: OnTarget600 at May 27, 2010, 06:21 AM

        I am still having a major issue however of all large files getting corrupted on download.

        Anybody? I still cannot resolve this issue.

        Thanks.
          • 26971
          • 3 Posts
          Quote from: MediaGuy at Jan 11, 2010, 10:20 AM

          Just to let you guys know: I changed "return" to "exit" and now my custom snippet is working, I think the same would work with the original FiledownloadPE!

          yes it does. You saved me a lot of heart ache thanks smiley
          Copy paste of original code with that simple exit change on download section
          <?php
          /**
           * @package FileDownloadPE (Pirate Edition)
           * @copyright 2008 MODx CMS/F Community.
           * @license GPL v3 <http://www.gnu.org/licenses/gpl.html>
           * @link http://scottydelicious.com/blog/2008/06/15/filedownloadpe
           * @version 1.1 <2008.18.06 : June 18th 2006>
           * @since 1.0 <2008.15.06 : June 15th 2006>
           * @category PHP 5 Only. Requires PHP5 <http://gophp5.org/>
           * @author Dr. Scotty Delicious, DFPA <[email protected]>
           */
          
          //  Set default snippet parameters.
          $_table     = 'downloads';
          $filename   = isset($filename)  ? $filename : null;
          $dberror    = isset($dberror)   ? $dberror  : 'There was an error processing your download request.';
          $nofile     = isset($nofile)    ? $nofile   : 'No file specified.';
          
          // Defaults for Template Variables:
          $alias = $_REQUEST['q'];
          if (!$alias) $alias = Download::GetAlias($_REQUEST['id']);
          $fileMimeTV = isset($fileMimeTV) ? $fileMimeTV : 'FileMime';
          $filePathTV = isset($filePathTV) ? $filePathTV : 'File';
          
          // Get download count for requested document.
          if ($action == 'count')
          {
          	$filename = isset($filename) ? $filename : Download::GetAlias($id);
              return Download::Count($filename, $_table, $dberror, $nofile);
          }
          
          // Download the requested file.
          $mime       = $modx->getTemplateVarOutput($fileMimeTV);
          $mime       = $mime[$fileMimeTV];
          $file       = $modx->getTemplateVarOutput($filePathTV);
          $file       = $modx->config['base_path'] . $file[$filePathTV];
          
          return Download::File($alias, $file, $mime, $_table, $dberror, $nofile);
          
          class Download
          {
              private static $error = '';
              private static $nofile = '';
              private static $table = '';
              private static $filename = '';
              
              public static function File($filename, $path, $mime, $table, $dberror, $nofile)
              {
                  global $modx;
                  self::$table = $modx->getFullTableName($table);
                  self::$filename = $filename;
                  self::$error = $dberror;
                  self::$nofile = $nofile;
                  
                  $check = self::_tableCheck($table);
                  if (!$check) return self::$error;
                  
                  $info = self::_getInfo();
                  if (!$info) return self::$error;
                  
                  if(is_file($path))  
                  {  
                      $count = ($info['count'] + 1);
                      $update = $modx->db->update("`count`=$count", self::$table, "`name`='$filename'");
          
                      // required for IE  
                      if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
          
                      header('Pragma: public');
                      header('Expires: 0');
                      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  
                      header('Cache-Control: private',false);  
                      header('Content-Type: '.$mime);  
                      header('Content-Disposition: attachment; filename="'.basename($path).'"');  
                      header('Content-Transfer-Encoding: binary');  
                      header('Content-Length: '.filesize($path));
                      readfile($path);
                      exit;
                  }
                  else
                  {
                      return self::$error;
                  }
              }
              
              public static function Count($filename, $table, $dberror, $nofile)
              {
                  if (!$filename) return $nofile;
                  global $modx;
                  self::$table = $modx->getFullTableName($table);
                  self::$filename = $filename;
                  self::$error = $dberror;
                  self::$nofile = $nofile;
                  
                  $check = self::_tableCheck($table);
                  if (!$check) return self::$error;
                  
                  $info = self::_getInfo();
                  if (!$info) return self::$error;
                  else return $info['count'];
              }
          
          	public static function GetAlias($id = 0)
          	{
          		global $modx;
          		if (!$id) return false;
          		$site_content = $modx->getFullTableName('site_content');
          		$document = $modx->db->select('alias', $site_content, "`id` = $id");
          		$alias = $modx->db->getRow($document);
          		return $alias['alias'];
          	}
              
              private static function _getInfo()
              {
                  global $modx;
                  $search = $modx->db->select('*', self::$table, "`name` = '" .self::$filename. "'");
          
                  if ( $modx->db->getRecordCount($search) == 0 )
                  {
                      $insert = array('name' => self::$filename, 'count' => 0);
                      if (!$modx->db->insert($insert, self::$table)) return false;
                      $search = $modx->db->select('*', self::$table, "`name` = '" .self::$filename. "'");
                  }
                  $info = $modx->db->getRow($search);
                  return $info;
              }
              
              private static function _tableCheck($tablename)
              {
                  global $modx;
                  $prefix = $modx->db->config['table_prefix'];
                  $check = "SHOW TABLES LIKE '" . $prefix . $tablename . "'";
                  $result = $modx->db->query($check);
                  $recordCount = $modx->db->getRecordCount($result);
                  if ($modx->db->getRecordCount($result) == 0)
                  {
                      $sql = "CREATE TABLE `" . $prefix . "downloads` (
                        `id` int(11) NOT NULL auto_increment,
                        `name` varchar(128) NOT NULL default '',
                        `count` int(11) NOT NULL default '0',
                        PRIMARY KEY  (`id`),
                        KEY `name` (`name`)
                      )";
                      if ( !$modx->db->query($sql) ) return false;
                      else $recordCount = 1;
                  }
                  return $recordCount;
              }
          }
          ?>

          This works! laugh
          • That’s because it’s "return"-ing from inside a function, so it will just "return" out of the function back into the main snippet code. It should return some kind of error code, which the main snippet can then determine whether to do something about or just return itself. Using exit() from inside of a function like that is not normally a good idea. A function should always return something to whoever called it.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 32396
              • 56 Posts
              Hi,
              where can i find Documentation to FileDownloadPE ?

              P.S. here is NO any docs http://scottydelicious.com/blog/2008/06/15/filedownloadpe
                Using: MODx Evo 1.0.6 / Revo 2.2.5