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
https://forums.phpfreaks.com/topic/15948-modrewrite-in-php-code/#findComment-65508
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
https://forums.phpfreaks.com/topic/15948-modrewrite-in-php-code/#findComment-65536
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.