8 Replies - 6711 Views - Last Post: 18 December 2008 - 03:43 AM Rate Topic: -----

#1 msvp4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-July 08

opening ,editing and saving text files using php

Posted 17 December 2008 - 12:27 AM

hi ,

I have found out this code from some other site and what i am actually trying to do is opening a file and then editing it in and then saving it

i have posted the code here

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

$filename="./test.txt";
$fd=fopen($filename,"r");
$textFileContents=fread($fd,filesize($filename));
//fclose($fd);
echo "$textFileContents"; 

if ($frmSub) {
$fd=fopen("./test.txt","w");
fwrite($fd, $newdata);
fclose($fd);
}
?>

<html>
<body>
<form action="<? echo $PHP_SELF; ?>" >
<textarea name="newdata" rows="10" cols="40" value="<?php echo $textFileContents; ?>">
<?
echo stripslashes($newdata);
?>
</textarea>

<input type="submit" name="frmSub" value="submit">
</form>

</body>
</html>





here in this code i am unable to display the contents of the file and then modify it and then save it

thanks,
msvp4u

Is This A Good Question/Topic? 0
  • +

Replies To: opening ,editing and saving text files using php

#2 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: opening ,editing and saving text files using php

Posted 17 December 2008 - 12:33 AM

Ok where do I begin....

You aren't declaring where the variables are coming from. Your form has no method at all, which will make this whole script useless. I added the method="POST" to fix that, and a few other things:

Your environment variables have no clue what they are. You are using variables that have no previous declarations. I added a few statements to figure out what variables are what, and where they come from.

Also, you were trying to use PHP to echo data to the page, when the <HTML> and <BODY> tags hadn't even been opened yet. There's another fatal error in itself.

I'm not sure where you got this code from, because it's all messed up. Check the date and PHP version of scripts before downloading them... but then again from the looks of this script, even the first versions of PHP wouldn't of supported this, as there just isn't enough information given to do what you needed to do.


I've fixed a bunch of stuff, so:

Try this:

<html>
<body>
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);



$filename = "./test.txt";
$fd = fopen($filename,"r");
$textFileContents = fread($fd,filesize($filename));
fclose($fd);

echo "$textFileContents";

$writedata = $_GET['newdata'];

if ($_POST['frmSub']) {
$fd=fopen("./test.txt","w");
fwrite($fd, $writedata);
fclose($fd);
}
?>


<form action="<? echo $PHP_SELF; ?>" method="POST" >
<textarea name="newdata" rows="10" cols="40">
<?
echo stripslashes($textFileContents);
?>
</textarea>

<input type="submit" name="frmSub" value="submit">
</form>

</body>
</html>




I haven't tested it, so let me know how it goes.

This post has been edited by pr4y: 17 December 2008 - 12:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 msvp4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-July 08

Re: opening ,editing and saving text files using php

Posted 17 December 2008 - 12:47 AM

i am able to get the contents of the file but when i press submit button the following error displays


The requested URL /ftp/<br /><b>Notice</b>: Undefined variable: PHP_SELF in <b>/var/www/html/ftp/modified.php</b> on line <b>26</b><br /> was not found on this server.
Was This Post Helpful? 0
  • +
  • -

#4 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: opening ,editing and saving text files using php

Posted 17 December 2008 - 12:54 AM

oh LOL...

i overlooked that one:

change to:

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST" >




Seriously, WHERE DID YOU FIND THIS CODE?

Sorry, but I had to fix something on almost every line!
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,328
  • Joined: 10-May 07

Re: opening ,editing and saving text files using php

Posted 17 December 2008 - 12:54 AM

Instead try $_SERVER['PHP_SELF']
Was This Post Helpful? 0
  • +
  • -

#6 msvp4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-July 08

Re: opening ,editing and saving text files using php

Posted 17 December 2008 - 05:18 AM

how to handle larger size files being displayed
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: opening ,editing and saving text files using php

Posted 17 December 2008 - 03:55 PM

If you mean you're getting an error that PHP is running out of memory for script execution, open up your php.ini file and edit the "memory_limit" line. Default is 16M (Don't see why you'd have a text file larger than 16 megabytes, but still).

If you're working with text files that big, have you considered using MySQL databases? Or is that not an option for this application?
Was This Post Helpful? 0
  • +
  • -

#8 msvp4u  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-July 08

Re: opening ,editing and saving text files using php

Posted 17 December 2008 - 09:37 PM

the following code works fine when the file which i am trying to edit in the same folder as that of the working directory
but when i try to change the directory and work on it i am unable to do it. tell me where i am going wrong

<?php
$folder_new=$_GET['folder'];
$file_new=$_GET['file'];
?>
	<html>

	<body bgcolor="black" text="white" link="yellow" vlink="yellow">
	
	<?php
$change=chdir("/var/www/html/$folder_new");
echo $folder_new;
echo $file_new;
	$rows = "20";
	$cols = "80";
   $action = $_REQUEST['action'];
   $file = $_REQUEST['file'];
	
   $file = trim($file);
   $file2 = $file;
   $save = "wh" . $action;
   $content = $_REQUEST['content'];

echo $change;
 //  echo "content = " . $content . "\n";
 //  echo "action = " . $action . "\n";
 //  echo "save = " . $save . "\n";
   echo "file = " . $file . "\n";
 //  echo "file2 = " . $file2 . "\n";
   if ($save == "whon") {
	   //Write to the file
	   $len = strlen(nl2br($content));
	   if ( !$FMM = fopen("$file2", "w") )
	   {
		   die ("Could Not Open File!");
	   }
	   flock($FMM, 2);
	   rewind($FMM);
	   if ( fwrite($FMM, $content, $len) === FALSE)
	   {
		   die ("Could Not Write, check permissions of file!");
	   }
	   flock($FMM, 3);
	   fclose($FMM);
   }
   if ($file == "") {
	   //set $file2 to be a blank file
	   $content = "\n[content]\n\n[end]\n\n";
	   echo "Filename was blank. Setting content to be new file. \n";
   }
 //  echo "file = " . $file . "\n";
 //  echo "file2 = " . $file2 . "\n";
   if (file_exists($file2)) {
	   $FMM = fopen("$file2", "r+");
	   $content = fread($FMM, filesize($file2));
	   $content = trim($content);
   } else {
	   echo $file2 . "does not exist. Setting content to be blank. ";
	   $content = "\n[content]\n\n[end]\n\n";
   }
   ?>

   <form action="fileEdit.php?file=<?php echo $file_new;?>&&folder=<?php echo $folder_new;?>" method="post"><center>
	   Filename: <input type="text" name="file" value="<?php echo $file; ?>"> <input type="checkbox" name="action"> Save <input type="submit" value="Update"><br>
	   <textarea wrap="virtual" name="content" cols="<?php echo $cols; ?>" rows="<?php echo $rows; ?>"><?php echo "$content"; ?></textarea><br>
   </center></form>
   </body>
   </html>


Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: opening ,editing and saving text files using php

Posted 18 December 2008 - 03:43 AM

I'm headed out for work, so I only had a moment to skim your code, but for what you're doing, would file_get_contents() and file_put_contents() be more useful for you?

With you dropping the content of the file into a textarea for editing, this might actually be easier for you, as it reduces the length of code you'd have to write, as well as the complexity.

I try to only use fopen() and friends if I have something complex I need to do that requires micromanagement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1