peter_anderson Posted January 22, 2009 Share Posted January 22, 2009 Hey, Can anyone help me with this little script? I've written a script for a client, but they want an additional part added. What they want is a file (eg index.php?) and if visited with a subpart to the URL (eg index.php?a=123) it would load a file called 123 in the directory. They are new to coding and don't want to have to add a new file with all the formatting. So, it is sort of like php include, but it needs to take the ?a=NAME from the url and add the NAME.txt file. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/141968-include-file-name/ Share on other sites More sharing options...
gevans Posted January 22, 2009 Share Posted January 22, 2009 <?php //image your url is index.php?a=the_name $filename = $_GET['the_name']; include("directory_to_your_file/{$filename}.txt"); That is very simple, you might want to do some checks that the file actually exists Quote Link to comment https://forums.phpfreaks.com/topic/141968-include-file-name/#findComment-743357 Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 <?php $file = isset($_GET['a'])?basename($_GET['a']):index; if (!file_exists($file)) $file = "index"; include($file . ".php"); ?> Should do it. Use caution though, I took steps in there to prevent someone from loading a different file, but I am not guaranteeing that at all, I would prefer to use an array of defined files that are allowed and if $file is in that array then include it. This is an example of that: <?php $file = isset($_GET['a'])?basename($_GET['a']):index; $allowed = array("index", "home", "contact"); if (!file_exists($file)) $file = "index"; elseif (!in_array($file, $allowed)) $file = "index"; include($file . ".php"); ?> That should be the safer version. Quote Link to comment https://forums.phpfreaks.com/topic/141968-include-file-name/#findComment-743359 Share on other sites More sharing options...
peter_anderson Posted January 22, 2009 Author Share Posted January 22, 2009 Thanks everyone I've been coding for a few weeks and I was desperate to get it finshed Quote Link to comment https://forums.phpfreaks.com/topic/141968-include-file-name/#findComment-743365 Share on other sites More sharing options...
peter_anderson Posted January 22, 2009 Author Share Posted January 22, 2009 Well, I tried it, but I can't get it working I'm more used to ASP and HTML, so excuse all my problems. The file name should be "news.php" and accessing it by "news.php?a=1" should bring up "1.txt". Any ideas how to best do it? <?php $file = isset($_GET['a'])?basename($_GET['a']):index; $allowed = array("index", "home", "contact"); if (!file_exists($file)) $file = "index"; elseif (!in_array($file, $allowed)) "Sorry but there are no files of that name to view at this time. Please check back later."; include($file . ".php"); ?> Is the code I used. But, it tries to download the script. Any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/141968-include-file-name/#findComment-743386 Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 <?php $file = isset($_GET['a'])?basename($_GET['a']):index; $allowed = array("index", "home", "contact"); if (!file_exists($file)) $file = "index"; elseif (!in_array($file, $allowed)) $file = "index"; // this line is here so we show something, it defaults to index if there is a problem. You also did not use the "echo" command for the "Sorry... so it was an invalid deal. include($file . ".php"); ?> See comments above. Quote Link to comment https://forums.phpfreaks.com/topic/141968-include-file-name/#findComment-743388 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.