Jump to content


Photo

ModRewrite in PHP code


  • Please log in to reply
4 replies to this topic

#1 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 29 July 2006 - 02:14 PM

strange question

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


Tell me the problem, I will try tell you the solution

#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 29 July 2006 - 02:23 PM

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:
www.site.com/index.php/Tutorials/Tutorial1
And then the variable $_SERVER['PATH_INFO'] will look like this:
Array
(
    [1] => Tutorials
    [2] => Tutorial1
)


<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 29 July 2006 - 04:07 PM

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]
Tell me the problem, I will try tell you the solution

#4 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 29 July 2006 - 04:25 PM

You can use this 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);
}
If you put it in a page called download and you by example requested www.site.com/download.php?file=Example.zip 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 www.site.com/download.php/Example.zip
<?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);
}

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#5 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 29 July 2006 - 05:08 PM

Excellent, Thank you
If you are a lass I will kiss ya

If not, then a pint will do.
Thanks again
Tell me the problem, I will try tell you the solution




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users