Jump to content

Recommended Posts

Is there a way that I can get the source of a .php file. Not what's in the file, but what the file outputs. I tried using file_get_contents() but that just gets the server-side code. I want to be able to get what the code outputs, Like exactly what you'd see if you viewed the page's source. Then store it in a variable, I can't find a solution to this problem.

Link to comment
https://forums.phpfreaks.com/topic/128463-solved-getting-a-phps-page-source/
Share on other sites

if you use file_get_contents() on the URL of the file (not local path) you will get the output of the file... or if you want it in the current scope you can include() it into a buffer and clean/close the buffer. It's likely that you are approaching the problem incorrectly though, why would you need to do this?

Oh, if you want the source of the file, it has to be on your server, it's impossible to get someone else's php code because it's parsed by their server before it's sent to you....

 

nono... I don't want the source. Like if someone had a file:

 

<?php

echo "Hello.<br />hello";

?>

 

When you right click > View page source.

 

You'd see.

 

Hello.
hello

 

Or whatever. That's what I want.

Oh, if you want the source of the file, it has to be on your server, it's impossible to get someone else's php code because it's parsed by their server before it's sent to you....

Agreed your need FTP access or a add handler update if its your code then use FTP if its not then write your own

 

EDIT: for the HTML source

use $googleshtml = file_get_contents('http://www.google.com/');

 

ie my file dummy.php is on the root of mydomain.com

so instead of $html = file_get_contents('dummy.php'); you would do $html = file_get_contents('http://mydomain.com/dummy.php');

 

make sense ?

Okay, let's go slowly. Is this file on your server or on someone else's website? If it's on yours, try doing this..

 

<?php
function getExecContent($file) {
   ob_start();
   require($file);
   return ob_get_clean();
}

$contents = getExecContent('/the/path/to/my/file.php');

 

If it's on someone else's (like google's), try doing what I said before...

 

$contents = file_get_contents('http://www.google.com/');

 

but this will still retrieve

"Hello.<br />hello"

not

"Hello.
hello"

If there was a prize for the largest number of confused people in a thread, this one would be a front-runner.

 

You start by asking how you get the source code - which can't be done unless you have server access to the domain where the script resides. Then you switch to wanting to see the generated html which can be done by 'view source' using a browser.

 

Could you clarify just what you want to get and how you're going to use it if you could get it.

Okay, let's go slowly. Is this file on your server or on someone else's website? If it's on yours, try doing this..

 

<?php
function getExecContent($file) {
   ob_start();
   file_get_contents($file);
   return ob_get_clean();
}

$contents = getExecContent('/the/path/to/my/file.php');

 

If it's on someone else's (like google's), try doing what I said before...

 

$contents = file_get_contents('http://www.google.com/');

 

It's on someone else's server. When I just try what you said I get this:

 

failed to open stream: Connection timed out in /home/a1280292/public_html/onlinetfg.php on line 3

 

However, the page works. And if I try to get it in like a html frame it works. Someone said that it's not working because it's trying to get the actual server-side code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.