Jump to content

ModRewrite in PHP code


onlyican

Recommended Posts

Rewrite is Apache-based so you can only do it in Apache .htaccess/config.

However, there's a PHP alternative which will allow you to use some thing like this:
[b]www.site.com/index.php/Tutorials/Tutorial1[/b]
And then the variable [b]$_SERVER['PATH_INFO'][/b] will look like this:
[code]
Array
(
    [1] => Tutorials
    [2] => Tutorial1
)[/code]

Link to comment
Share on other sites

What I am looking to do is have a link
<a href='123456.zip'>Download</a>
So they download a file, and it is called 123456.zip

But the file name on the server is actually myzip.zip
Simular to mod rewrite rule
RewriteRule ^123456.zip myzip.zip [L]
Link to comment
Share on other sites

You can use this code:
[code]
<?php
function send_file($path, $display)
{
    if (!is_file($path) or connection_status()!=0)
        return(FALSE);

    header("Content-type: unknown/unknown");
    header("Content-Disposition:  attachment; filename=\"$display\"");
    header("Pragma: public");
    header("Cache-control: private");
    header("Content-transfer-encoding: binary\n");
    header("Content-length: ".(string)(filesize($path)));
    header("Expires: 0");
    header("Pragma: no-cache");

    if ($file = fopen($path, 'rb'))
    {
        while(!feof($file) and (connection_status()==0))
        {
            print(fread($file, 1024*300));
            flush();
        }
        $status = (connection_status()==0);
        fclose($file);
    }
    return($status);
}
$requested_file = (( isset($_GET['file']) )?($_GET['file']):(''));

// Fake filename => Real Filename
$rewrite = array(
'Example.zip'=>'1234.zip'
,'Tutorial.zip'=>'x401TPT.zip'
);

if(array_key_exists($requested_file,$rewrite))
{
    send_file($rewrite[$requested_file],$requested_file);
}
[/code]
If you put it in a page called download and you by example requested [b]www.site.com/download.php?file=Example.zip[/b] it will send you 1234.zip which will appear as example.zip
By adding entries to the rewrite array you can add different file rewrites.

Also, you could use this version to use a URL like [b]www.site.com/download.php/Example.zip[/b]
[code]
<?php
function send_file($path, $display)
{
    if (!is_file($path) or connection_status()!=0)
        return(FALSE);

    header("Content-type: unknown/unknown");
    header("Content-Disposition:  attachment; filename=\"$display\"");
    header("Pragma: public");
    header("Cache-control: private");
    header("Content-transfer-encoding: binary\n");
    header("Content-length: ".(string)(filesize($path)));
    header("Expires: 0");
    header("Pragma: no-cache");

    if ($file = fopen($path, 'rb'))
    {
        while(!feof($file) and (connection_status()==0))
        {
            print(fread($file, 1024*300));
            flush();
        }
        $status = (connection_status()==0);
        fclose($file);
    }
    return($status);
}

$pinfo = (( isset($_SERVER['PATH_INFO']) )?($_SERVER['PATH_INFO']):(exit));

$pExpl = explode('/',$pinfo);
unset($pExpl[0]);

$requested_file = array_pop($pExpl);

// Fake filename => Real Filename
$rewrite = array(
'Example.zip'=>'1234.zip'
,'Tutorial.zip'=>'x401TPT.zip'
);

if(array_key_exists($requested_file,$rewrite))
{
    send_file($rewrite[$requested_file],$requested_file);
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.