file_get_contents() returns incomplete data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Acetylene
Forum Newbie
Posts: 4
Joined: Mon May 18, 2009 12:14 pm

file_get_contents() returns incomplete data

Post 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.
Last edited by Benjamin on Mon May 18, 2009 12:59 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: file_get_contents() returns incomplete data

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: file_get_contents() returns incomplete data

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Acetylene
Forum Newbie
Posts: 4
Joined: Mon May 18, 2009 12:14 pm

Re: file_get_contents() returns incomplete data

Post 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.
Post Reply