0

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?

2
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by DemonGal711
0

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

0

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.

Edited by DemonGal711: n/a

0

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.

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.