spindrift Posted May 31, 2006 Share Posted May 31, 2006 Hello!I have this code in template.php...[code]<?php @ require_once ("$pagename.php"); ?>[/code]...to bring content into the template.I've got a folder called 'news' for all the news articles so how could I edit the code to say 'look for files in the news folder as well as the current folder'?I guess I would need something like:[code]<?php @ require_once ("$pagename.php" or "news/$pagename.php"); ?>[/code]...but that doesn't work. How would I change it to work?Thank you very much and hope to hear from you.Mark Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/ Share on other sites More sharing options...
Orio Posted May 31, 2006 Share Posted May 31, 2006 First question:[code]<?php$file=$pagename;$file.=".php"@require_once ($file);[/code]I didnt really understand the second question... The file is either in root folder or in /news/ ? Or is it in both?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40531 Share on other sites More sharing options...
AndyB Posted May 31, 2006 Share Posted May 31, 2006 This was already answered in another thread you started.The solution is to use the file_exists() function [see the php manual] to check for the presence of the file you want in (say) the root folder. If it exists, include it; if it doesn't exist check with file_exists() again in the /news folder. Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40551 Share on other sites More sharing options...
spindrift Posted May 31, 2006 Author Share Posted May 31, 2006 [!--quoteo(post=378676:date=May 31 2006, 07:05 AM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ May 31 2006, 07:05 AM) [snapback]378676[/snapback][/div][div class=\'quotemain\'][!--quotec--]First question:[code]<?php$file=$pagename;$file.=".php"@require_once ($file);[/code]I didnt really understand the second question... The file is either in root folder or in /news/ ? Or is it in both?Orio.[/quote]Hi Orio and Andy,Many thanks for the replies - much appreciated.The file will be in either the _root folder or in /news/. Do you know how I could code it?Many thanks and hope to hear back from you.All the best,Mark Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40609 Share on other sites More sharing options...
wildteen88 Posted May 31, 2006 Share Posted May 31, 2006 Is it me or did Andy not explain how to! I will quote his post:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]The solution is to use the [b]file_exists()[/b] function [see the php manual] to check for the presence of the file you want in (say) the root folder. If it exists, include it; if it doesn't exist check with file_exists() again in the /news folder.[/quote]Heres a link to manual that shows you how to use the [a href=\"http://uk2.php.net/manual/en/function.file-exists.php\" target=\"_blank\"]file_exists[/a] function.But any I will provide you the code:[code]//check in the root folder firstif(file_exists('./' . $filename . '.php')){ include './' . $filename . 'php';}//if it wasn't found in the root folder then check in the news folderelseif(file_exisits('./news/' . $filename . '.php')){ include './news/' . $filename . '.php';}// if it couldn't be found display messageelse{ echo $filename . '.php could not be found in either the root folder or the news folder!';}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40651 Share on other sites More sharing options...
spindrift Posted June 1, 2006 Author Share Posted June 1, 2006 hi wildteen88,that's great, thank you so much for that!the code works fine for loading files from the _root directory but when it needs to pull something from the /news/ folder, i get the following error message...Fatal error: Call to undefined function: file_exisits() in /var/www/html/clients/npr/main.php on line 40...that line of syntax is... [code]elseif(file_exisits('./news/' . $filename . '.php')){ include './news/' . $pagename . '.php';}// if it couldn't be found display messageelse{ echo $pagename . '.php could not be found in either the root folder or the news folder!';}[/code]do you by any chance know why this could be happening? i've tried a few small things like taking out the full-stop infront of ('./news/' etc. but to no avail unfortunately.thank you very much once again and i'd be grateful if you could get back to me at some point please.all the best,mark Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40879 Share on other sites More sharing options...
AndyB Posted June 1, 2006 Share Posted June 1, 2006 It's a typo ... change exisits to exists and it'll work. Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40884 Share on other sites More sharing options...
MikoMak Posted June 1, 2006 Share Posted June 1, 2006 Fatal error: Call to undefined function: file_exisits() in /var/www/html/clients/npr/main.php on line 40that's telling you a function by the name file_exis[b]i[/b]ts doesn't exist. Simple typo - change that to [b]file_exists[/b] () Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40885 Share on other sites More sharing options...
spindrift Posted June 1, 2006 Author Share Posted June 1, 2006 hi guys,thanks for that - boy, do i look stupid!i've corrected the typo but now everytime it tries to call something from the /news/ folder, it always come up with a message saying "060308.php could not be found in either the root folder or the news folder!".i've checked that 060308.php is definately in /news/ so i don't understand it. Does any of you guys have any suggestions at all?thanks again for all your help.mark Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40898 Share on other sites More sharing options...
AndyB Posted June 1, 2006 Share Posted June 1, 2006 Can you post the version of code that you are using now, please. Quote Link to comment https://forums.phpfreaks.com/topic/10848-how-can-i-write-this-shortsimple-bit-of-php-so-it-will-work/#findComment-40920 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.