onlyican Posted July 29, 2006 Share Posted July 29, 2006 strange questionCan I write a ReWrite rule in a PHP scriptwithout having to use the code to add/edit a .htaccess file Quote Link to comment https://forums.phpfreaks.com/topic/15948-modrewrite-in-php-code/ Share on other sites More sharing options...
ShogunWarrior Posted July 29, 2006 Share Posted July 29, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/15948-modrewrite-in-php-code/#findComment-65508 Share on other sites More sharing options...
onlyican Posted July 29, 2006 Author Share Posted July 29, 2006 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.zipBut the file name on the server is actually myzip.zipSimular to mod rewrite ruleRewriteRule ^123456.zip myzip.zip [L] Quote Link to comment https://forums.phpfreaks.com/topic/15948-modrewrite-in-php-code/#findComment-65530 Share on other sites More sharing options...
ShogunWarrior Posted July 29, 2006 Share Posted July 29, 2006 You can use this code:[code]<?phpfunction 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.zipBy 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]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/15948-modrewrite-in-php-code/#findComment-65536 Share on other sites More sharing options...
onlyican Posted July 29, 2006 Author Share Posted July 29, 2006 Excellent, Thank youIf you are a lass I will kiss yaIf not, then a pint will do.Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/15948-modrewrite-in-php-code/#findComment-65545 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.