Adding ".php" to the result of a $_REQUEST['*']

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Adding ".php" to the result of a $_REQUEST['*']

Post by mattcooper »

Hi all,

Being a relative newcomer to PHP, I hope I will be forgiven for asking for the answer to what I think is a very easy question!! I am trying to write a simple script that creates new pages on the fly and populates them with the contents of an include file that holds the standard layout for all pages (navigation, contents div etc).

The script will be getting the name of the new page via a $_REQUEST. I would like to know how you append the result with ".php" and store the result as $pagename; so, for example, the name for the new page is "books". The user submits the form, and the form handler gets the name "books" with the $_REQUEST['pagename'] bit. I then want the script to add ".php" to books, and store "books.php" as $pagename.

The code will then go on to check if that filename exists and, if it doesn't, will create it. It will then open the file "includes.inc" and copy the contents over to "books.php", thereby creating an entirely new, empty page on the fly... brilliant!

But how to I append ".php", and while you're at it, how do I copy the contents of "includes.inc" to $pagename?? :)

Thanks in advance, as usual!!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

$pagename = $_REQUEST['pagename'].'.php';
but you might want to use $_GET or $_POST instead of $_REQUEST from now on since it will be easier to undertand whats going on in larger codes
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Brilliant, that works nicely. So, here's the code as it is at the moment:

Code: Select all

$pagename = $_POST['pagename'].'.php';
$includes = fopen ("includes.php","r");

$fp = fopen("$pagename", "w+");
fputs($fp,$includes);
fclose($includes);
The page gets created, but it doesn't have the includes in it; all the file contains are the words "Resource ID #1".

Why would that be?

Thanks for your help, I'm gradually getting there!!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

what are you trying to do in the end? put the $includes file into the $pagename file? if so you gotta fread() the contents of the $includes file and then put THAT into the $pagename file, not the actually opened file itself but the contents of the opened file cuase you can't put a file in a file
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

Yes, that's precisely what's supposed to happen. Would you be so kind as to edit my code?? ;)

This function is a new one to me!!

Thanks again.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

i will never do this again but ok fine

Code: Select all

$pagename = $_POST['pagename'].'.php';
$includes = fopen ("includes.php","r");
$contents = fread($includes, filesize('includes.php'));

$fp = fopen("$pagename", "w+");
fwrite($fp,$contents);
fclose($includes);
untested
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

You, Sir, are a generous and very kind person. It works perfectly, 100 genius points are winging their way to you!

It all makes sense to me, I just didn't know about the fread() function. You learn something new every day, they say!

Thank you very much indeed, hope to speak with you again sometime.

Matt.
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

you can also use file_get_contents()

Code: Select all

$contents = file_get_contents('includes.php');
A lot simpler and less error_prone.
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

ryanlwh wrote:you can also use file_get_contents()

Code: Select all

$contents = file_get_contents('includes.php');
A lot simpler and less error_prone.
Exactly what I was going to suggest. And you don't even need to use the fread()function - when you use file_get_contents("file.ext") then what you get is a string that contains what's practically in the file!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

what is faster though? file_contents_things or fread and hings?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

shiznatix wrote:what is faster though? file_contents_things or fread and hings?
why don't you run some benchmarks and post your results :wink:
Post Reply