Page 1 of 2
file_get_contents($file)???!!
Posted: Mon Jun 26, 2006 11:22 am
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.
Posted: Mon Jun 26, 2006 11:54 am
by RobertGonzalez
Could be anything without knowing what your problem is

. Can you elaborate a bit?
Posted: Mon Jun 26, 2006 11:59 am
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!
Posted: Mon Jun 26, 2006 12:00 pm
by tsalexey544
LINE 9 $first = gettxt(\first.txt);
LINE 7 file_get_contents($file) or die('Could not read file!');
Posted: Mon Jun 26, 2006 12:04 pm
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.
Posted: Mon Jun 26, 2006 12:09 pm
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
?>
Posted: Mon Jun 26, 2006 12:39 pm
by RobertGonzalez
Have you tried removing the leading slash?
Posted: Mon Jun 26, 2006 12:41 pm
by tsalexey544
I get a blank page(the txt file has TEXT in it)
Posted: Mon Jun 26, 2006 12:48 pm
by RobertGonzalez
Code: Select all
<?php
function gettxt($file)
{
if (!$return = file_get_contents($file))
{
return false;
}
return $return;
}
echo gettxt('first.txt');
?>
Posted: Mon Jun 26, 2006 12:50 pm
by tsalexey544
Everah!!!! you did it THANKS!!!! you saved me!!
But what were my mistakes?
Posted: Mon Jun 26, 2006 1:50 pm
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.
Posted: Mon Jun 26, 2006 2:20 pm
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.
Posted: Mon Jun 26, 2006 3:11 pm
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');
?>
Posted: Tue Jun 27, 2006 1:22 am
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?
Posted: Tue Jun 27, 2006 1:25 am
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?