Page 1 of 1

file_get_contents() returns incomplete data

Posted: Mon May 18, 2009 12:32 pm
by Acetylene
Hi,

I'm just learning to work with file contents in PHP and I'm having some trouble with file_get_contents. It doesn't return the entire contents of the (very simple) file.

When trying to get this file:

Code: Select all

 
<?php
$productName = 'Sample Product 1';
$types = array(0 => array('type' => 'Ground, 250g',
    'price' => 495,
    'weight' => 250,
    'frv' => 3,
    'category' => 'standard'),
  1 => array('type' => 'Whole Bean, 250g',
    'price' => 595,
    'weight' => 250,
    'frv' => 3,
    'category' => 'limited',
    'limit' => 4),
  2 => array('type' => 'Free Sample, 100g',
    'price' => 0,
    'weight' => 100,                                    
    'frv' => 3,
    'category' => 'sample'));
?>
 
This code:

Code: Select all

 
$filetxt =file_get_contents("products/sample_product.php");
echo "file_get_contents: " . $filetxt;
 
Returns this (in html output):
file_get_contents: array('type' => 'Ground, 250g', 'price' => 495, 'weight' => 250, 'frv' => 3, 'category' => 'standard'), 1 => array('type' => 'Whole Bean, 250g', 'price' => 595, 'weight' => 250, 'frv' => 3, 'category' => 'limited', 'limit' => 4), 2 => array('type' => 'Free Sample, 100g', 'price' => 0, 'weight' => 100, 'frv' => 3, 'category' => 'sample')); ?>

When I use file() instead, it does manage to get all the lines, but I want to use file_get_contents() and, more importantly, I want to know WHY this isn't working. If I change the filetype to .txt it gets the entire contents, but I thought it was supposed to be possible to get .php files. I want to be able to work with files ending in .php so I can also use it as an include file.

Please let me know if there's any more information I can give you. This really has me stumped and I'd appreciate any help.

Re: file_get_contents() returns incomplete data

Posted: Mon May 18, 2009 1:01 pm
by Benjamin
file_get_contents() won't parse or evaluate the contents of a file, regardless of the extension. View the source code for the page you are viewing. I'm sure the entire contents of the file are there.

Re: file_get_contents() returns incomplete data

Posted: Mon May 18, 2009 1:22 pm
by s.dot
It depends if the php is being parsed before its getting the contents of the file. For example file_get_contents('http://domain.tld/file.php'); will get the contents after php has been parsed and the output is sent. However, if you do file_get_contents('/path/to/file.php'), I don't think it runs through the php parser first.

Re: file_get_contents() returns incomplete data

Posted: Wed May 20, 2009 1:07 pm
by Acetylene
Yeah, you're right, it was in the source. I feel pretty dumb now. Thanks for helping me, I probably never would have realized.