I was just testing out a code and I found something rather pathetic.


This code does not produce anything.

<?php
$source = file_get_contents("http://mysite.com/random/page.html");
echo $source;
?>

But this code does

<?php
$source = file_get_contents("page.html");
echo $source;
?>

The php script and page.html are in the same folder, so perhaps that's the issue, but I don't see why it should make a difference if I give it the full path name or not. Am I missing something?

You're not giving a full path, you're giving a web path. In the first example it is making a web request to fetch the file, in the second example it is directly accessing the filesystem. Make sure your path is correct or use an absolute path like /path/to/your/webroot/page.html

Alright, so what would be the proper way to get information from a web path then?

The above code was what I was told in class yet it produced nothing when I tried to echo the teachers example page. My original code was to use a web path but since it wasn't producing any output, I decided to see what the issue was.

Alright, so what would be the proper way to get information from a web path then?

The above code was what I was told in class yet it produced nothing when I tried to echo the teachers example page. My original code was to use a web path but since it wasn't producing any output, I decided to see what the issue was.

If the file is on the same server as the script then use the absolute path. If it's a remote file use the URL.

Alright. I think I get it. So, if I were to try and echo the contents of http://www.example.com/ , I would use this for my code right? Cause, at the moment I'm not seeing anything.

<?php
$source = file_get_contents("http://www.example.com/");
echo $source;
?>

Sidenote: We have to use a variable and echo it. I know this could be a one line code.

Alright. I think I get it. So, if I were to try and echo the contents of http://www.example.com/ , I would use this for my code right? Cause, at the moment I'm not seeing anything.

<?php
$source = file_get_contents("http://www.example.com/");
echo $source;
?>

Sidenote: We have to use a variable and echo it. I know this could be a one line code.

If you see absolutely nothing then you may be getting an error. On the line before $source = ... put ini_set('display_errors', 'On'); error_reporting(E_ALL); If you're getting an error that will show it

Well, that helped. Apparently URL file access is disabled. Thanks.

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.