Jump to content

Recommended Posts

Hello all  ;D

 

I'm testing out the fopen function.

 

<textarea id="elm4" name="index_info" rows="1" cols="1" style="width: 643px; height: 550px;">
<?php 
if(isset($_POST['save']))
{
$myFile = "index_info.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$index_info";
fwrite($fh, $stringData);
fclose($fh);

}
include_once 'index_info.php';
?>
</textarea>
<input type="submit" name="save" value="Save Settings" class="button"/>
</form>

 

What I'm trying to get done is view the current information in the file (ie. index_info.php), and if I decide to hit save it will update the file with whatever info I put in it.

 

The problem is that it is deleting the info in the page and its not doing anything else.

Link to comment
https://forums.phpfreaks.com/topic/137096-solved-fopen/
Share on other sites

'w'  Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

 

'a'  Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

 

Seems pretty straight forward to me. Use a+ if the file may not exist to open the file to write/append to it. Use a if you know the file exists and want to append to it, use just regular a.

 

Using w, places the file pointer (where you start writing to the file) at the begining and truncates (deletes) the rest of the file so it is fresh.

Link to comment
https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716109
Share on other sites

I thought I did, anyway:

 

<form method="post" action="pages.php">
<textarea id="elm4" name="index_info" rows="1" cols="1" style="width: 643px; height: 550px;">
<?php 
if(isset($_POST['index_info']))
{
$myFile = "../index_info.php";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = "$index_info";
fwrite($fh, $stringData);
fclose($fh);

}
include_once '../index_info.php';
?>
</textarea>
<input type="submit" name="save" value="Save Settings" class="button"/>
</form>

 

So I just gave it a 2nd try with "a+" it doesn't delete my information on my page but it doesn't update neither. Thats weird.

Link to comment
https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716138
Share on other sites

<?php 
$myFile = "../index_info.php";
if(isset($_POST['index_info'])) {
   $fh = fopen($myFile, 'w') or die("can't open file");
   fwrite($fh, $_POST['index_info']);
   fclose($fh);
}
?>

<form method="post" action="pages.php">
<textarea id="elm4" name="index_info" rows="1" cols="1" style="width: 643px; height: 550px;"><?php readfile($myFile); ?></textarea>
<input type="submit" name="save" value="Save Settings" class="button"/>
</form>

 

should work, try that and if it doesn't, tell us. If it does, try to figure out how it differs from yours and what made the difference. (ps. you might want to look into file_put_contents() )

Link to comment
https://forums.phpfreaks.com/topic/137096-solved-fopen/#findComment-716199
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.