Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

..argument not a valid stream resource

Status
Not open for further replies.

gosuc2000

Technical User
Jun 2, 2004
59
DE
Hello List,

being new to php, I have some problems writing the content of a textarea into a file. In my case, I always geht the error
rewind() errmsg
fputs()errmsg
fclose()errmsg ,
each functionname in the errormessage is followed by
"supplied argument is not a valid stream resource in file ... on line .."

Here is the excerpt of my code:

.
.
<form action="<php rcho $PHP_SELF; ?> method="post">
.
.
<table....
.
<td><textarea name="messages" rows=3 cols=40 scroll=yes wrap=soft></textarea><td>
..
.
</form>

$file="comment.txt"
if (isset($_POST[`messages']))
{
$pointer=fopen($_POST['file'],"r+"); <<<above error here
$_POST['messages']=stripslashes(nl2br($_POST['messages']));
rewind ($_POST['pointer']);
fputs($_Post['pointer',$_POST['messages'); <<<above error here
fclose($_POST['pointer']); <<<above error here
}
?>


I can't see right now, what is wrong with the code.
Can anybody help me? Any help is apreciated.

Thanks for your help in advance.

Best regards,

Fred
 
What is in your $_POST['pointer']. Without that, we can hardly help you. But...

1. variables in php are case sensitive. That means that $_Post is not the same as $_POST
2. you can't use backtick and single quote to delimit index in an array as you do it here: if (isset($_POST[`messages']))
3. when you begin a bracket, you need to close it as well, not like here: fputs($_Post['pointer',$_POST['messages');

Try correcting errors like this:
Code:
if (isset($_POST['messages']))
{
  $pointer = fopen($_POST['file'],"r+");
  $_POST['messages'] = stripslashes(nl2br($_POST['messages']));
  rewind ($_POST['pointer']);
  fputs ($_POST['pointer'],$_POST['messages']);
  fclose ($_POST['pointer']);
}
Still, we need to know what is in the pointer.
 
Hi Vragabond,

thank you for your rapid response. I have to apologize for not taking the time to verify what I typed in. The errors you showed me are not in the original script, and I tried to translate from German text into English 'on the fly' - sorry.
I tried to check the content of the message-variable
after submitting the form, and the content was there. What I could not find was the content of the pointer.

This time I cut and paste the text of the script in.

(Mitteilungen = message)

<tr>
<td>Mitteilungen an uns:</td>
<td><textarea name="Mitteilungen" rows=3 cols=40 scroll="yes" wrap="soft"></textarea></td>
</tr>
.
.
.
<?php
$datei="comment.txt"; <<< datei= filename
if (isset($_POST['Mitteilungen']))
{
echo "Einsprung erfolgreich ! \n" . $_POST['Mitteilungen']; <<< messagecontent was succesfully typed
$zeiger=fopen($_POST['datei'],"r+"); << zeiger = pointer
if (isset($_POST['datei']))
{
echo "pointer-value is: " . $_POST['zeiger'];<<no output!!
}
$_POST['Mitteilungen']=stripslashes(nl2br($_POST['Mitteilungen']));
rewind($_POST['zeiger']);
fputs($_POST["zeiger"],$_POST["Mitteilungen"]);
fclose($_POST["zeiger"]);
}
?>

Hope this is better this time
Please excuse my poor English

regards,

Fred
 
Hi,

I found another error in the script.

I changed
$zeiger=fopen($_POST['datei'],"r+");
if (isset($_POST['datei'])) <<< error datei = filename
{
echo "Zeigerwert ist: " . $_POST['zeiger'];
}


into


$zeiger=fopen($_POST['datei'],"r+");
if (isset($_POST['zeiger'])) <<< correct zeiger = pointer
{
echo "Zeigerwert ist: " . $_POST['zeiger'];<<< print pointervalue , it didn't print anything !!!!!
}


regards

Fred
 
I solved my problem ...

I had to use the $_POST.. notation for ALL values.

to set the file-variable it must be
$_POST['datei']="comment.txt";

For the pointer it has to be
$_POST['zeiger']=fopen($_POST['datei'],"r+");

Under this notation I also received the pointer-value.

Perhaps a final question. I thought I could use the notation without $_POST .. etc, if I "stay" on the same script for getting the form-data.
But if I have to use this notation, does this have to do something with the php-variables??

Regards,
Fred
 
It seems like you do not understand the use of the superglobal array $_POST. It contains the values that have been sent from the previous page via a post method on the form. It does not hold variables that you create in the current script. So, by looking at your code, I guess it would make sense to do:
Code:
<?php
  $datei = "comment.txt";
  if (isset($_POST['Mitteilungen']))
  {
    echo "Einsprung erfolgreich ! \n" . $_POST['Mitteilungen'];
    $zeiger = fopen($datei,"r+");
    if ($zeiger)
    {
      $_POST['Mitteilungen'] = stripslashes(nl2br($_POST['Mitteilungen']));
      rewind ($zeiger);
      fputs ($zeiger, $_POST["Mitteilungen"]);
      fclose ($zeiger);
    }
  }
?>
 
Hello Vragabond,

thank you for the code-example and the explanation
about global arrays. I used your example and the problem is gone. But I will read again the use of global arrays, to better understand what I'm doing.

Again,
thanks for the advise.

regards,

Fred
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top