2

I'm trying to get the contents of a specific website, write the content to a .txt file and output unformatted result in my browser. I'm able to actually write the website content to my text file, but it won't echo out in my browser. I tried using "w+" and "r+" in my original handle but that didn't do the trick. What am I doing wrong here?

$file = file_get_contents('http://www.example.com');
$handle = fopen("text.txt", "w");
fwrite($handle, $file);
fclose($handle);

$myfile = "text.txt";
$handle = fopen($myfile, "r");
$read = htmlentities(fread($handle, filesize($myfile)));
echo $read;
4
  • You're code runs fine on my machine. Commented Dec 17, 2012 at 11:43
  • Can't see anything wrong with the code. Is test.txt correctly populated after the first block of code? If not, probably a permissions issue. Commented Dec 17, 2012 at 11:44
  • 1
    Is there a good reason for not simply doing echo htmlentities(file_get_contents('http://www.example.com')); ? Commented Dec 17, 2012 at 11:45
  • The reason is simply that I'm trying to learn the technique, so I do it the (slightly) hard(er) way :) Commented Dec 17, 2012 at 12:10

3 Answers 3

1

Your initial statement of $file contains all of the formatted code from the file you just wrote so you can use that.

 echo htmlentities($file);

However, several people have asked this already.. perhaps the OP wants to verify that the file was written correctly?

Your code for opening looks fine, have you ruled out a permissions issue? check this by using is_readable($myFile) prior to opening it. You can also do an is_writable on the folder that your writing to to ensure that your actually writing the file.

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

2 Comments

The file is written correctly, and I stumbled upon something interesting just now. Example.com actually works, but that wasn't the URL I tried to fetch in my real code. Seems the code works well with some sites, but not with others. For example, google.com does not work. Any ideas?
Some sites can block requests such as file_get_contents. Google.com will because it will very quickly classify you as a spammer - I have ran into that way way too often. You can get around this blocking by identifying as a browser. Lookup curl which will allow you to do that. As you said above your main aim is to learn and this will facilitate that end goal brilliantly.
1

You don't need to open the files multiple times .. just try

$file = "log.txt";
$url = 'http://www.stackoverflow.com' ;

$handle = fopen("log.txt", "w+");
fwrite($handle, file_get_contents($url));
rewind($handle);

$read = htmlentities(fread($handle, filesize($file)));
echo $read;

1 Comment

Thanks! That simplifies things.
0

You can var_dump() the $handle and see if it can be opened. If yes, var_dump($read)..

And to read files you can also use file_get_contents().

Comments

Your Answer

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