Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:17:34+00:00 2026-05-28T03:17:34+00:00

I want to write a drupal module that serves static files, but I want

  • 0

I want to write a drupal module that serves static files, but I want it to serve them from the address http://www.mydomain.com/moduleName/fileName Where FileName could be anything (e.g. javascript/app.js or photos/imageA.jpg). I don’t want to copy the files to that actual location if possible, ideally I want the module to just hotlink the two locations.

I tried using a menu with a callback that looks like
$numargs = func_num_args();
$arg_list = func_get_args();

$fileName = drupal_get_path('module', 'moduleName') . "/files";
for ($i = 0; $i < $numargs; $i++) {
    $fileName = $fileName . "/" . $arg_list[$i];
}
if ($numargs == 0) {
    $fileName = drupal_get_path('module', 'moduleName') . "/files/index.html";
}

if (file_exists($fileName)) {
    header('Content-Type: ' . getFileMimeType($fileName));
    $handle = fopen($fileName, "r");
    $contents = fread($handle, filesize($fileName));
    fclose($handle);
    echo $contents;
} else {
    drupal_json_output(array('exists' => file_exists($fileName), 'fileName' => $fileName));
}
drupal_exit();

Where getFileMimeType looks like

function rcsarooms_getFileMimeType($file) {
    $extension = pathinfo($file, PATHINFO_EXTENSION);
    if ($extension == "html" || $extension == "md") {
        $type = "text/html; charset=utf-8";
    } else if ($extension == "css") {
        $type = "text/css";
    } else if ($extension == "js") {
        $type = "application/x-javascript";
    } else if ($extension == "json") {
        $type = "application/json";
    } else if ($extension == "ico") {
        $type = "image/x-icon";
    } else if ($extension == "png") {
        $type = "image/png";
    } else {
        $type = "charset=UTF-8";
        echo $extension;
        drupal_exit();
        return;
    }
    return $type;
}

but while this works fine for any text files it fails completely for any binary files (e.g. png, ico etc.) I also don’t like having to specify the mime-type like that.

The question is, can custom_url_rewrite do what I need to do and if so what should the custom url rewrite function have in it and where should it go?

I’ve tried this, but it doesn’t seem to work:

function custom_url_rewrite($op, $result, $path) {
    if (strpos($result, "data") === false) {
        if ($op == 'alias') {
            if (preg_match('|^' . drupal_get_path('module', 'myModule') . '/files/(.*)|', $path, $matches)) {
                return 'myModule/' . $matches[1];
            }
        }

        if ($op == 'source') {
            if (preg_match('|^myModule/(.*)|', $path, $matches)) {
                return drupal_get_path('module', 'myModule') . '/files/'.$matches[1];
            }
        }
    }

    return $result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:17:34+00:00Added an answer on May 28, 2026 at 3:17 am

    I just found this function on php.net which does work (although I’m still interested to know if anyone else has a better way of doing this)

    function readfile_chunked( $filename) { 
        $chunksize = 1 * (1024 * 1024); // how many bytes per chunk 
        $buffer = ''; 
        $cnt = 0; 
        $handle = fopen( $filename, 'rb' ); 
        if ( $handle === false ) { 
            return false; 
        } 
        ob_end_clean(); //added to fix ZIP file corruption 
        ob_start(); //added to fix ZIP file corruption 
        header( 'Content-Type:' ); //added to fix file corruption 
        header('Cache-Control: max-age=7200'); //time in seconds. so cache for 2 hours
        while ( !feof( $handle ) ) { 
            $buffer = fread( $handle, $chunksize ); 
            echo $buffer; 
            ob_flush(); 
            flush(); 
        } 
        fclose( $handle );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to write modules by myself very much. but for drupal and php,
I want to write a drupal module which will toggle between 2 themes according
I want to write Drupal Form value made from API Form, to my database
I'm writing a drupal module, and I need to write a query that returns
I want to write a Swing application in Griffon but I am not sure
I want to write a command that specifies the word under the cursor in
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an
I want to write a word addin that does some computations and updates some
I want to write some JavaScript that will change the onmousedown of a div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.