Re: .htaccess style rewrite with php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe

    Re: .htaccess style rewrite with php

    On Aug 5, 8:28 pm, Sjoerd <sjoer...@gmail .comwrote:
    On 5 aug, 18:29, Joe <j...@faceh.com wrote:
    >
    the issue with that is how to return image
    files, or pdf files, or .doc files, etc. as that file type.
    >
    You have to provide some headers, like Content-Type, Content-Length,
    Content-Disposition. Here's an example:
    >
    <?php
    class FileServer {
    function serve($path) {
    $mime = self::getMimeTy pe($path);
    $size = self::getFilesi ze($path);
    header('Content-Type: '.$mime);
    header('Content-Length: '.$size);
    readfile($path) ;
    }
    >
    function getMimeType($pa th) {
    $extension = strtolower(subs tr(strrchr($pat h, "."),
    1));
    if ($extension == 'jpg') return 'image/jpeg';
    if ($extension == 'jpeg') return 'image/jpeg';
    if ($extension == 'png') return 'image/png';
    return `file -bi $path`; // uses the 'file' command,
    available on unix.
    }
    >
    function getFilesize($pa th) {
    return filesize($path) ;
    }}
    >
    ?>
    That looks pretty promising, I'll give it a try, thanks!
  • Joe

    #2
    Re: .htaccess style rewrite with php

    On Aug 18, 5:04 pm, Joe <j...@faceh.com wrote:
    On Aug 5, 8:28 pm, Sjoerd <sjoer...@gmail .comwrote:
    >
    >
    >
    On 5 aug, 18:29, Joe <j...@faceh.com wrote:
    >
    the issue with that is how to return image
    files, or pdf files, or .doc files, etc. as that file type.
    >
    You have to provide some headers, like Content-Type, Content-Length,
    Content-Disposition. Here's an example:
    >
    <?php
    class FileServer {
    function serve($path) {
    $mime = self::getMimeTy pe($path);
    $size = self::getFilesi ze($path);
    header('Content-Type: '.$mime);
    header('Content-Length: '.$size);
    readfile($path) ;
    }
    >
    function getMimeType($pa th) {
    $extension = strtolower(subs tr(strrchr($pat h, "."),
    1));
    if ($extension == 'jpg') return 'image/jpeg';
    if ($extension == 'jpeg') return 'image/jpeg';
    if ($extension == 'png') return 'image/png';
    return `file -bi $path`; // uses the 'file' command,
    available on unix.
    }
    >
    function getFilesize($pa th) {
    return filesize($path) ;
    }}
    >
    ?>
    >
    That looks pretty promising, I'll give it a try, thanks!
    I can get this working in firefox and safari, but I'm having problems
    with PDF's in Internet Explorer. I had it working at one stage, but
    the filename of the downloaded file was showing as the URL of the page
    accessed, rather than the filename of the file. Here is my code:

    $file = base_path()."te st.pdf";
    if (file_exists($f ile))
    {
    // Get mime type
    $mime = function-here-to-get-mime-type;

    // Force download?
    if ($mime == 'application/x-download') {
    $disposition = "attachment ";
    }
    else {
    $disposition = "inline";
    }

    // Load asset
    $name = basename($file) ;
    header('Cache-Control: no-cache');
    header('Content-transfer-encoding: 8bit');
    header('Content-Length: '.filesize($fil e));
    header('Content-Type: '.$mime.'; name="'.$name.' "');
    header('Content-Disposition: '.$disposition. '; filename="'.
    $name.'"');
    readfile($file) ;
    exit();
    }

    Comment

    Working...