file_get_contents($file)???!!

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

tsalexey544
Forum Commoner
Posts: 41
Joined: Thu Jun 22, 2006 11:19 am

file_get_contents($file)???!!

Post by tsalexey544 »

I can't understand what is wrong......=(
plz help me.

Code: Select all

<?php

function gettxt($datafile)
{
file_get_contents($file) or die('Could not read file!');
}
$first = gettxt(\first.txt);
echo $first

?>
I get this error:
I get this


Warning: Unexpected character in input: '\' (ASCII=92) state=1 in E:\MiniComp\04 - Media\02 PROGRAMMING\html\Child care\finish\php559.tmp on line 9

Notice: Use of undefined constant first - assumed 'first' in E:\MiniComp\04 - Media\02 PROGRAMMING\html\Child care\finish\php559.tmp on line 9

Notice: Use of undefined constant txt - assumed 'txt' in E:\MiniComp\04 - Media\02 PROGRAMMING\html\Child care\finish\php559.tmp on line 9

Notice: Undefined variable: file in E:\MiniComp\04 - Media\02 PROGRAMMING\html\Child care\finish\php559.tmp on line 7
Could not read file!


------------------------------------
LINE 9 $first = gettxt(\first.txt);
LINE 7 file_get_contents($file) or die('Could not read file!');
--------------------------------------

What I want to do is to change the content of my site by changing the txt file. If you have a better why to do it plz tell me.
Last edited by tsalexey544 on Mon Jun 26, 2006 12:02 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Could be anything without knowing what your problem is :wink: . Can you elaborate a bit?
tsalexey544
Forum Commoner
Posts: 41
Joined: Thu Jun 22, 2006 11:19 am

Post by tsalexey544 »

I get this


Warning: Unexpected character in input: '\' (ASCII=92) state=1 in E:\MiniComp\04 - Media\02 PROGRAMMING\html\Child care\finish\php559.tmp on line 9

Notice: Use of undefined constant first - assumed 'first' in E:\MiniComp\04 - Media\02 PROGRAMMING\html\Child care\finish\php559.tmp on line 9

Notice: Use of undefined constant txt - assumed 'txt' in E:\MiniComp\04 - Media\02 PROGRAMMING\html\Child care\finish\php559.tmp on line 9

Notice: Undefined variable: file in E:\MiniComp\04 - Media\02 PROGRAMMING\html\Child care\finish\php559.tmp on line 7
Could not read file!
tsalexey544
Forum Commoner
Posts: 41
Joined: Thu Jun 22, 2006 11:19 am

Post by tsalexey544 »

LINE 9 $first = gettxt(\first.txt);
LINE 7 file_get_contents($file) or die('Could not read file!');
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

three problems: $datafile and $file, You're expecting gettext() to return data, yet you don't tell it to return anything, \first.txt should be in quotes as you want it to be a string.
tsalexey544
Forum Commoner
Posts: 41
Joined: Thu Jun 22, 2006 11:19 am

Post by tsalexey544 »

and how it is now?

Code: Select all

<?php

function gettxt($file)
{
file_get_contents($file) or die('Could not read file!');
}
$first = gettxt('\first.txt');
echo $first

?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you tried removing the leading slash?
tsalexey544
Forum Commoner
Posts: 41
Joined: Thu Jun 22, 2006 11:19 am

Post by tsalexey544 »

I get a blank page(the txt file has TEXT in it)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
function gettxt($file)
{
    if (!$return = file_get_contents($file))
    {
        return false;
    }

    return $return;
}

echo gettxt('first.txt');
?>
tsalexey544
Forum Commoner
Posts: 41
Joined: Thu Jun 22, 2006 11:19 am

Post by tsalexey544 »

Everah!!!! you did it THANKS!!!! you saved me!!

But what were my mistakes?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Basically what Feyd said. The file_get_contents function reads the file into a string. If you don't set the function equal to something, it will read the file into an unknown string. So I coded the function to do that. If it fails, return a value of false. If it succeeds, the function returns the string that the file was read into.

Hope that helps.
tsalexey544
Forum Commoner
Posts: 41
Joined: Thu Jun 22, 2006 11:19 am

Post by tsalexey544 »

Last question. =)
How will the script look with out the "If it fails"? I tried to get rid of it but it doesn't work.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I would recommend always accounting for a return, so I would probably do something like this if you wanted to not check against failure.

Code: Select all

<?php
function gettxt($file)
{
    if ($return = file_get_contents($file))
    {
        return $return;
    }

    return false;
}

echo gettxt('first.txt');
?>
tsalexey544
Forum Commoner
Posts: 41
Joined: Thu Jun 22, 2006 11:19 am

Post by tsalexey544 »

Not this. I meant a script like this....

Code: Select all

function gettxt($file)
{
    $return = file_get_contents($file);
}
echo gettxt('first.txt');
why is this script doesn't work?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

tsalexey544 wrote:Not this. I meant a script like this....

Code: Select all

function gettxt($file)
{
    $return = file_get_contents($file);
}
echo gettxt('first.txt');
why is this script doesn't work?
Because the function doesn't return anything. The function is setting the string value of the $file into the var $return. But $return is only available within the scope of the function, so trying to access it is impossible outside of the function.

Using echo to try to output the value of the function the way you have it written will display blank because there is truly no value to the function.

What you want to do with a function is use it to return a value. Then you can use a var or echo to grab/display the value returned by the function.

Does this make any sense?
Post Reply