1

Is there a way to display the source code of somepage.php when you go to somepagesrc.php? IOW, is there a PHP function that will in somepagesrc.php output the text contents of the file (or source code) somepage.php?

I understand I could copy the contents of somepage.php into somepage.html but that doesn't cut it for me because I want it to be dynamic so that I don't have to copy the code over every time I make a change.

1
  • Try header content type plain/text Commented Mar 29, 2013 at 3:14

4 Answers 4

3

Maybe use file_get_contents along with header('Content-type: text/plain') - but obligatory security warning - be sure to filter the names of files so people can't include more than you intent.

E.g.:

<?php

header('Content-type: text/plain');

print file_get_contents("somepage.php");

?>
Sign up to request clarification or add additional context in comments.

8 Comments

Obligatory comment regarding sending the correct mime type. PHP source mime type != "text/html"
header('Content-type: application/x-httpd-php'); or header('Content-type: application/x-httpd-php-source');. Preferably the latter but either is correct. There're others that are correct. For more info on mime types check out /etc/mime.types on a Unix machine. Make sure you call the header function before any output is sent!
yes but that causes an open-with dialog -- i want to stream it into the browser kind of like with `<pre></pre>' i tried pre but it didn't work
Wouldn't you be able to print the contents as plain text? print file_get_contents('file.php'); with appropriate headers?
That's exactly what you'd do. Call header to send the correct mime type then print/echo the file's contents.
|
0

or you could use Reflection through which you could easily get functions, variables, and even comments.

http://www.php.net/manual/en/intro.reflection.php

Comments

0

Try easily to read its contents as any file using the file manipulation functions :) I don't know if that may be the solution you are searching about :$

Comments

0

Take a look at highlight_file(). It not only grabs the php source to display (like file_get_contents()) but nicely formats/colorizes the code for output.

http://php.net/manual/en/function.highlight-file.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.