Hello all!
My problem is: I have 2 web php pages, in a online server. 1 is listing database content, the other is to "download" the 1º but in html...

I'm doing this by getting the content of 1º page, in a link like http:\mysite.com\page.php, to make the page run and output all in "html". If I used the local name page "page.php" it simply returned the php code (all).

The problem is that i tried to get the content with cUrl, file_get_content, file, fread.. nothing works! and if I put htmlentities, will return just javascript that is on body of that page...

And... in local, all works! And I searched and checked if allow_url_open is on and it is!

$content = file($myFile);
foreach ($content as $lines) {
echo htmlentities($lines);
}

I would say there is too little description of your problem. The loop you are posting here does not relate to any fetching of a URL.

To debug, I would do something like
$google = file_get_contents('http://www.google.com'); echo $google;

If it displays some sort of a Google start page, it means that file_get_contents works and URL wrappers are allowed. Be aware though that you will get more than just the "BODY" part of a page. You will get the HEAD as well which might have some impact on the display.

As said, tell us a little bit more, like a larger snippet of your code.

Sorry for little details.
I made the file_get_contents you posted and it worked...then i put my url and it didn't.

My url to get content of is like http://www.mysite.com/ip/page.php?id=79&tipo=site
Even I try to delete the querystrings...

I think I have enought permissions on that file, because I can open it...

More detailed code:

$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80")
{
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} 
else 
{
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}

 $x= str_replace("con.php","page.php",$pageURL);
$myFile =$x;

if ($fp = fopen("page.php", 'r')) { // I try to get it by local, it returns all php code
   $content = '';

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

    while ($line = fread($fp, filesize("page.php"))) {
            $content .= htmlentities($line);
 echo "<p>can read url $myFile<p>$content";
   }

//other method but with URL
$content = file($myFile);
foreach ($content as $lines) {
echo htmlentities($lines);
}

The url has the ip of the person who clics to download the page, so I have to get the current url, and then I replace the name of the current for the other that I want to download (index.php).

Sorry if I couldn't explain... thanks

Member Avatar for LastMitch

@ainosilva

Have you try to used str_replace function?

<?php
function curPageURL() {
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

$current_url = str_replace("www.", "", curPageURL());
echo $current_url;
?>

yes, same result :/

Member Avatar for LastMitch

@ainosilva

so I have to get the current url, and then I replace the name of the current for the other that I want to download (index.php).

I am bit confused.

Do you mean you want to write it in a text file? Writing the current url in a text file?

No. I have a .php page that I want to get content from to write in another page and make a download for the user.

Member Avatar for LastMitch

@ainosilva

No. I have a .php page that I want to get content from to write in another page and make a download for the user.

You can't used .php file in fopen(). It won't read and write that .php file. You have to substitute .php file with $page_file

Instead of this:

$x= str_replace("con.php","page.php",$pageURL);
$myFile =$x;
if ($fp = fopen("page.php", 'r')) { // I try to get it by local, it returns all php code
$content = '';
$google = file_get_contents('http://www.google.com'); echo $google;
while ($line = fread($fp, filesize("page.php"))) {
        $content .= htmlentities($line);
echo "<p>can read url $myFile<p>$content";
}

You have to used this

$page_file = page.php
$con_file = con.php
$myFile = $x;

$x= str_replace("$con_file","$page_file",$pageURL);

if ($fp = fopen("$page_file", 'r')) { // I try to get it by local, it returns all php code

$content = '';

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

while ($line = fread($fp, filesize("$page_file"))) {

$content .= htmlentities($line);

echo "<p>can read url $myFile<p>$content";
}   

that's the only way it will read and write that .php file

The reason why I mention .txt file because it's easily to read and write strings on .txt file

nop... the result is the same... :/ I don't understand why in localhost it works...
on line 15, if I put htmlentities only appears a script that is on body, if I take that off, appears all the php code...

In local it works putting open the $x (url) and with no htmlentities... if I do that on the server the result is: only appears that script (javascript)... :S

Member Avatar for LastMitch

@ainosilva

nop... the result is the same... :/ I don't understand why in localhost it works...
on line 15, if I put htmlentities only appears a script that is on body, if I take that off, appears all the php code...

It's bit confusing now. I thought you want to read and write on the .php file? But your intention now is different. I guess there was mis-communication. Everything is working fine as you mention but:

You just want

$content .= htmlentities($line);

to work?

When you used $htmlentities

you have to include $str

It should be

$content .= htmlentities($str, $line);

...hmm sorry, but in this case what is $str value? :$

I think you should try with urlencode($your_response);
This will work for sure...

no... :/ it didn't

Member Avatar for diafol

Not sure what you're getting at but if you're trying to get php code out of a remote file - you can't. The php file that you request will run and return the content 'to display'. The only way to circumvent this is if the remote site has saved php code into files that do not have a php extension. This would be quite a strange thing to do.

Of course the following on your localhost:

echo '<pre>' . htmlentities(file_get_contents('index.php')) . '</pre>';

will spit out all the code as if it was a regular text file.

I am very pleased that this is not possible on a remote server, otherwise all my php and SQL queries code could be ripped off and checked for security holes. Gulp!

I put the url, so I can get only the "html" code, because the file with the content that I want to is in .php and it's in my server (same as the page with the code above)...
so is there any other way to do it? :/

I'm thinking (desperately) in making an huge string with all the code of the other page and run, and split and make it like html... if there isn't another way

Thank you for all answers until now :)

Member Avatar for diafol

You've confused me - I'm not getting much of this. You're calling a file from the broswer (via url querystring) and you want to display some specific php code to the screen - is that right?

I have 2 files. One have php code to display some information. Another calls the other to get the content into a string, to create another one but .html.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.