example of how to use fopen function in php write to files

Mobile
feeds
Welcome Login | Register

example of how to use fopen function in php write to files
Reply Your Comments:
Click this button if you are interested in replying to this topic and leave your comments
Sent To Friend
CLick this button if you want to send this page to a friend.
Subsribe To Rss Feeds
Subscribe to RSS
CLick this button if you want to subscribe to this RSS Feed. You can use your browsers feeds burners if you have mozilla or internet explorer 7 or higher and keep up with updates.
  Forums Index
      » Scripts Forums
        » » example of how to use fopen function in php write to files
example of how to use fopen function in php write to files
Post Description: example of how to use fopen function in php write to files Scripts
Post Tags:
This Post Has Been Viewed 1501 Times Since Sat Jan 19, 2008 4:55 am Author hostman with 4 replies
example of how to use fopen function in php write to files
Advertise On This Page




here is a script i wrote to test how to edit files in your server with fopen funcion in php..

step 1: create a file called test.php and give it 777 permission (if linux) if windows, dont have to give permissions

step2. open notepad and copy and paste the following code and save it as fopen.php


<?
$filename = 'test.php';

if(isset($_REQUEST['Submit'])){
$somecontent = stripslashes($_POST['somecontent']);
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened fi<form action="" method="get"></form>le.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename) <a href=".$_SERVER['PHP_SELF']."> - Continue - ";

fclose($handle);

} else {
echo "The file $filename is not writable";
}
}
else{
// get contents of a file into a string

$handle = fopen($filename, "r");
$somecontent = fread($handle, filesize($filename));
fclose($handle);
?>
<h1>Edit file <? echo $filename ;?></h1>
<form name="form1" method="post" action="">
<p>
<textarea name="somecontent" cols="80" rows="10"><? echo $somecontent ;?></textarea>
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>

<?
}
?>
<p align="center">script by <a href="http://www.wallpaperama.com">wallpaperama.com</a></p>
<p align="center">support and hosting by <a href="http://www.webune.com">webune.com</a> </p>


now upload to your server and open it with your browser, you'll be able to edit the file with your browser. this is cool.


Leave Your Comments

Your Name
Your Email Address (Will Not Be Published)
Notify Me When Someone Replies to this Page
(An email will be sent to you when someone replies to your comments)
Your Comments
Include A Picture with your comments
Share
| More
Share this page by putting this URL in your comments to other websites like myspace, Facebook, Twitter friendster, Hi5, Groups, Boards, Forum or others. Just Copy and Paste this Code
URL:
To embed this topic, just copy the code from the "Embed" box. Once you've copied the code, just paste it into your website or blog to embed it.
Embed:
BBCODE is use on forums. You can put this code on all your BBCODE enabled forums like PhpBB, vBulletin® and others. Just Copy and Paste this code on your Posts and Replies on your forums
BBCODE:
Subscribe Feeds
Webmasters - Exchange Links With Us. Add related websites to this topic
Add Link:
Links Related to : example of how to use fopen function in php write to files

Comments and replies About example of how to use fopen function in php write to files
:: 1 :: Reply #38915 Reply By Sparky On Wed Jan 30, 2008 12:54 pm
Sparky:
thansk that really helped being looking for hours most i found would edit php but i needed to xml and yours worked where others all failed big thanks :-)
:: 2 :: Reply #43726 Reply By Saravanan On Fri Feb 29, 2008 11:59 pm
Saravanan:
good script. some error in file opening. remove the else statement for first if block.
:: 3 :: Reply #44475 Reply By webune On Thu Mar 06, 2008 2:03 pm
webune:
if you want to append the file instead of writing something new all you have to do is change line 12 of this script

from this:
if (!$handle = fopen($filename, 'w')) {


to this:
if (!$handle = fopen($filename, 'w+')) {
:: 4 :: Reply #96087 Reply By baltazar On Thu Mar 05, 2009 5:34 pm
baltazar:
05-5676-tasi.jpg
ok! and i k\like your tutuorials