Page 1 of 1

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

Posted: Fri Sep 16, 2005 9:01 am
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!!

Posted: Fri Sep 16, 2005 9:28 am
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

Posted: Fri Sep 16, 2005 9:50 am
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!!

Posted: Fri Sep 16, 2005 9:56 am
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

Posted: Fri Sep 16, 2005 9:59 am
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.

Posted: Fri Sep 16, 2005 10:06 am
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

Posted: Fri Sep 16, 2005 10:10 am
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.

Posted: Fri Sep 16, 2005 12:12 pm
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.

Posted: Fri Sep 16, 2005 12:22 pm
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!

Posted: Fri Sep 16, 2005 1:03 pm
by shiznatix
what is faster though? file_contents_things or fread and hings?

Posted: Fri Sep 16, 2005 1:23 pm
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: