Jump to content

Archived

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

onlyican

ModRewrite in PHP code

Recommended Posts

strange question

Can I write a ReWrite rule in a PHP script
without having to use the code to add/edit a .htaccess file

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Excellent, Thank you
If you are a lass I will kiss ya

If not, then a pint will do.
Thanks again

Share this post


Link to post
Share on other sites

×

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.