Jump to content
  • Advertisement
Sign in to follow this  
lexus727

PHP help

This topic is 4438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, im brand new 2 this site, but i desperatly need some php help, i didnt write this at all, i cant do php, but i need this desperatly on my site, herz teh error Warning: fclose(): supplied argument is not a valid stream resource in /home/elementa/public_html/lexus/process.p hp on line 10 AND the code <? $name = $_POST['name']; $site = $_POST['site']; $mail = $_POST['email']; // write data $f = 'data.txt'; fopen($f, 'a+'); fwrite($f, 'a+',$name . " | " . $site . " | " . $mail . " | " . "\n"); fclose($f); //dump it $open = file($f); foreach ($open as $keys => $values) { echo $keys . " | " .$values; echo "<br /> \n"; } ?> thanks a bunch guys,

Share this post


Link to post
Share on other sites
Advertisement
It should probably be something like:

<?
$name = $_POST['name'];
$site = $_POST['site'];
$mail = $_POST['email'];

// write data
$f = 'data.txt';
$handle = fopen($f, 'a+');
fwrite($handle, $name . " | " . $site . " | " . $mail . " | " . "\n");
fclose($handle);

//dump it
$open = file($f);

foreach ($open as $keys => $values) {
echo $keys . " | " .$values;
echo "<br /> \n";
}
?>


Read the documentation.


jfl.

Share this post


Link to post
Share on other sites
awesome, i just want it so that the user doesnt get redirected to the php, just gets a java error thats its been posted, herz the post code

<form action="process.php" method="post">
Name:
<input type="text" name="name" />
<br />
Site Name:
<input type="text" name="site" />
<br />
eMail:
<input type="text" name="email" />
<input type="submit" value="Submit">
</form>

thanks an enourmes amount!!

~lexus

Share this post


Link to post
Share on other sites
You would be better off just redirecting to the calling page from process.php. Ask yourself if it really is necessary to have a javascript message box.

Share this post


Link to post
Share on other sites
It would look something like this (not tested):
<head>
<?php
if( isset( $_GET['sender'] ) )
echo "<meta http-equiv=\"refresh\" content=\"2;url=" . $_GET['sender'] . "\">";
?>
</head>

And your form code would be:
<form action="process.php?sender=thispage.php" method="post">

The 2 in the above is the number of seconds you stay on process.php, where you can tell the user that the information has been posted.

Share this post


Link to post
Share on other sites
I guess I should make one or two points just in case:

1) IO routines are best done fast -- but when you're debugging, who cares about fast -- as long as they work. Hence, it's usually a good idea to wrap fopen calls inside conditionals.

So, with $f = 'data.txt';

if($file = fopen($f,"a+")
{
// if we managed to open $f, the resulting pointer to a file will be stored in $file.
// we can then manipulate data.txt through its pointer -- $file.
// If it didn't work, $file will return false and this conditional block never gets executed.

}
else
{
echo "Failed to open $f.";
}

So -- the point about reading the PHP docs is important -- if you don't know how fopen works (and it is quite simple really) then chances are when you use it...you'll get strange errors.
The reason you want to check if the fopen succeeded is this: if it didn't, any calls that refer to that file...will die -- and give you a message like the one you got.

In fact, the message you got means precisely this: That you tried to open/create a file for appending but that the operation was unsuccessful and the file resource (pointer to file) is NULL or false. Hence, when you tried to write to it...it complained, because the place you wrote to didn't exist (in PHP's view, anyway).

That said, while it is OK to rely on an implicitly returned file resource (pointer to file)...and thus use function calls like so fopen("blah.txt","r");...without assigning a result ($newfile = fopen("blah.txt","a+");)...it is a bad idea!

Why? Because in your example: fwrite($f, 'a+',$name . " | " . $site . " | " . $mail . " | " . "\n"); -- means this: write $name . " | " . $site . " | " . $mail . " | " . "\n" into file $f -- which is actually just a string saying 'data.txt' and not a file handle...furthermore, you've got 'a+' in the fwrite call which isn't required or will even let it work :)

So, go read the docs -- and while you are there, check out the Header() function.
Example -- header("Location: http://www.google.com"); -- sends HTTP headers telling the browser to redirect to google. HTTP headers themselves are a lengthy subject -- but the header function lets you do all sorts of cool stuff (refresh, redirect, put cookies etc).

~Shiny.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!