1

Im very new to drupal. Recently I have started learning drupal 6 with reference project done by my friend. It was having so many bugs and fixed most of from the list.

Still Im not getting how hook menu works. One module called filedepot installed which is for uploading and downloading files. When Im clicking on download button its redirecting to homepage. I think there is pblm in hook_menu

Download url : http://localhost/my_project/?q=filedepot_download/8/42

$items['filedepot_download/%node/%'] = array(
  'title' => 'File download',
  'page callback' => 'filedepot_download',
  'page arguments' => array(1, 2, 3, 4),
  'access callback' => 'filedepot_user_access',
  'access arguments' => array('access filedepot') ,
  'type' => MENU_CALLBACK,
  );

function call

function filedepot_download($node, $fid, $version, $mode='') {
  global $conf, $user;
  $filedepot = filedepot_filedepot();

  $field = content_fields('field_filedepot_file', $node->type);
  $db_info = content_database_info($field);

  $filepath = '';
  $content_disposition = 'attachment';

  if (empty($fid)) {
    watchdog('filedepot', "Download request - null file id");
    return drupal_access_denied();
  }
  elseif ($version == 'incoming') {
    $query = db_query("SELECT cckfid,orig_filename,title FROM {filedepot_import_queue} WHERE id=%d", $fid);
    list($cckfid, $fname, $filetitle) = array_values(db_fetch_array($query));
    if ($cckfid > 0) {
      $filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid=%d", $cckfid));
      if (file_exists($filepath)) {
        $result = db_query("SELECT * FROM {files} WHERE fid= %d", $cckfid);
        $file = db_fetch_object($result);
        $name = mime_header_encode($filetitle);
        $type = mime_header_encode($file->filemime);
        // By default, serve images, text, and flash content for display rather than
        // download. Or if variable 'filefield_inline_types' is set, use its patterns.
        $inline_types = variable_get('filefield_inline_types', array('^text/', '^image/', 'flash$'));
        $disposition = 'attachment';
        foreach ($inline_types as $inline_type) {
          // Exclamation marks are used as delimiters to avoid escaping slashes.
          if (preg_match('!' . $inline_type . '!', $file->filemime)) {
            $disposition = 'inline';
          }
        }
        $headers = array(
        'Content-Type: ' . $type . '; name="' . $filetitle . '"',
        'Content-Length: ' . $file->filesize,
        'Content-Disposition: ' . $disposition . '; filename="' . $filetitle . '"',
        'Cache-Control: private',
        );
        if (count($headers)) {
          watchdog('filedepot', 'Transferring incoming file %name (filedepot fid %fid, CCK fid %cckfid)',
            array('%name' => $filetitle, '%fid' => $fid, '%cckfid' => $cckfid), WATCHDOG_INFO);
          file_transfer($filepath, $headers);
        }

      }
      else {
        return drupal_not_found();
      }
    }
    else {
      watchdog('filedepot', "Download request for incoming file invalid");
      return drupal_access_denied();
    }

  }
  else {

    $version = intval($version);
    if ($version > 0) {
      $filetitle = db_result(db_query("SELECT title FROM {filedepot_files} WHERE fid=%d", $fid));
      $fname = db_result(db_query("SELECT fname FROM {filedepot_fileversions} WHERE fid=%d AND version=%d", $fid, $version));
      $cid = db_result(db_query("SELECT cid FROM {filedepot_files} WHERE fid=%d", $fid));
    }
    else if ($mode == 'moderator') {
      $query = db_query("SELECT cid,fname,tempname,title FROM {filedepot_filesubmissions} WHERE id=%d", $fid);
      $rec = db_fetch_array($query);
      if ($rec === FALSE) {
        watchdog('filedepot', "Download request for moderated file - invalid file reference");
        return drupal_access_denied();
      }
      list($cid, $fname, $tempname, $filetitle) = array_values($rec);
    }
    else {
      $query = db_query("SELECT cid,fname,title,version FROM {filedepot_files} WHERE fid=%d", $fid);
      $rec = db_fetch_array($query);
      if ($rec === FALSE) {
        watchdog('filedepot', "Download request - invalid file reference");
        return drupal_access_denied();
      }
      else {
        list($cid, $fname, $filetitle,$version) = array_values($rec);
      }
    }

    if ($cid == 0) {
      watchdog('filedepot', "Download request - null category id");
      return drupal_access_denied();
    }
    else {
      if ($mode == 'moderator') {
        $filepath = $filedepot->root_storage_path . "{$cid}/submissions/{$tempname}";
      }
      else {
        $filepath = $filedepot->root_storage_path . "{$cid}/{$fname}";
      }

      if ($filedepot->checkPermission($cid, 'view') === FALSE) {
        watchdog('filedepot', "Download request for incoming file invalid access to folder ($cid) for user: {$user->name}");
        return drupal_access_denied();
      }

      if (file_exists($filepath) AND !is_dir($filepath)) {
        $system_directory_path = $conf['file_directory_path'];
        $conf['file_directory_path'] = $filedepot->root_storage_path . $cid;
        $result = db_query("SELECT * FROM {files} WHERE filepath = '%s'", $filepath);

        if (!$file = db_fetch_object($result)) {
          // We don't really care about this file.
          watchdog('filedepot', "File record not found");
          return drupal_access_denied();
        }
        if ($mode == 'moderator') {
          $name = mime_header_encode($filetitle);
          $type = mime_header_encode($file->filemime);
        }
        else {
          if (db_result(db_query("SELECT COUNT(*) FROM {filedepot_downloads} WHERE uid=%d AND fid=%d", $user->uid, $fid)) == 0) {
            db_query("INSERT into {filedepot_downloads} (uid,fid,remote_ip,date) VALUES (%d,%d,'%s',%d)", $user->uid, $fid, $_SERVER['REMOTE_ADDR'], time());
          }
          $name = mime_header_encode($filetitle);
          $type = mime_header_encode($file->filemime);
        }
        // By default, serve images, text, and flash content for display rather than
        // download. Or if variable 'filefield_inline_types' is set, use its patterns.
        $inline_types = variable_get('filefield_inline_types', array('^text/', '^image/', 'flash$'));
        $disposition = 'attachment';
        foreach ($inline_types as $inline_type) {
          // Exclamation marks are used as delimiters to avoid escaping slashes.
          if (preg_match('!' . $inline_type . '!', $file->filemime)) {
            $disposition = 'inline';
          }
        }

        if ($mode == 'edit') { // User is using the download for edit function

          $ext = end(explode(".", $filetitle));
          $pos=strpos($filetitle, ".");

          // Generate a 15 character hash value (token) and append to filename
          // Create a unique filename for download and save the token for compare use on upload
          $filename=substr($filetitle, 0, $pos);
          $hash = md5(uniqid(rand()));
          $token=substr($hash, 0, 15);
          $newfilename=$filename . '{' . $token . 't}' . ".$ext";
          $newfilename=str_replace(' ', '+', $newfilename);

          //Update the MimeHeader that will be used to send the file to the browser - need to include token in filename
          $name = mime_header_encode($newfilename);

          $sql  = "INSERT INTO {filedepot_export_queue} (orig_filename,token,extension,timestamp,uid,fid) values ('%s','%s','%s',%d,%d,%d)";
          db_query($sql, $filetitle, $token, $ext, time(), $user->uid, $fid);
          // Change file status to locked - being edited
          db_query("UPDATE {filedepot_files} SET status = 2, status_changedby_uid = %d WHERE fid=%d", $user->uid, $fid);
        }

        $headers = array(
        'Content-Type: ' . $type . '; name="' . $name . '"',
        'Content-Length: ' . $file->filesize,
        'Content-Disposition: ' . $disposition . '; filename="' . $name . '"',
        'Cache-Control: private',
        );

        if (count($headers)) {
          watchdog('filedepot', 'Transferring file %name (filedepot fid %fid, CCK fid %cckfid)',
            array('%name' => $name, '%fid' => $fid, '%cckfid' => $file->fid), WATCHDOG_INFO);
          file_transfer($filepath, $headers);
        }
      }
      $conf['file_directory_path'] = $system_directory_path;
      return drupal_not_found();
    }
  }

}

Can anyone help me ? when Im clicking on download its redirecting to homepage

closed as off-topic by kiamlaluno Jul 11 '16 at 12:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    If your concern is about your hook not firing, you should place some debug statement inside filedepot_download to see if it's output to the site before/after the redirect. – Shawn Conn Apr 28 '15 at 7:02
  • thank for the reply, issue was upload path pblm – Shafeeque Apr 30 '15 at 7:40
1

I thought it was hook_menu not triggering correctly. But I was wrong, path were not correct. So the file was not there, that's it was redirecting to homepage

Finally I found the issue.

There was two reason

1. File Upload Path, We can change the path from http://yoursite.com/?q=admin/settings/filedepot
2. Upload Path Folder Permission. 

I hope this would help someone.

Not the answer you're looking for? Browse other questions tagged or ask your own question.