...

View Full Version : Problem of reading a line break from a textfile into a textarea



bhakti_thakkar
02-24-2007, 12:43 PM
hi all,
i have to load a bunch of data e.g termsandconditions from a textfile into a <textarea>. The problem is, as soon as there is a line break in that textfile it stops reading it and i get only half the data ie. [up to no breaks]. How will my php code read the line break and also put the same in my textarea????
Below is the code:
///////////////////////////////////////////
<?php
global $data;
global $x;
$data = "INSERT CODE HERE";
$x = 0;
if($_REQUEST['pf1'] != ""){
$hdl = fopen($_REQUEST['pf1'],'w');
fwrite($hdl, $_REQUEST['pft']);
fclose($hdl);
echo "WROTE FILE";
}
if(isset($_REQUEST['optsel'])){
$hdl = fopen($_REQUEST['optsel'],'rb');
global $data;
//global $data;
$data = fgets($hdl, filesize($_REQUEST['optsel']));
fclose($hdl);
}

function gtdr(){

$dir = "/myfolder";
$dh = opendir($dir);

echo "<html><head><title>HTML EDIT</title></head><body>
<form action='readfile.php?a=$eyeapp' method='post' name='oxel'>NAME:<input type='text' name='pf1'>";
echo "<select name='optsel' onchange=''>";
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
echo "<option value='$filename'>$filename";
}
echo "</select>";
echo "<br><input type='submit' name='pfs' value='Save'><input type='submit' name='pfo' value='Open'><input type='reset'><br><textarea rows='30' cols='86' name='pft'>";
global $data;
echo $data;
echo "</textarea></body></html>";
return "";
}
gtdr();
?>
//////////////////////// END OF CODE ///////////////////////////

thanks and god bless

shaileshpatil
02-24-2007, 12:49 PM
hi,
Please change your code as below and you will get your desired result :thumbsup:

<?php
global $data;
global $x;
$data = "INSERT CODE HERE";
$x = 0;
if($_REQUEST['pf1'] != ""){
$hdl = fopen($_REQUEST['pf1'],'w');
fwrite($hdl, $_REQUEST['pft']);
fclose($hdl);
echo "WROTE FILE";
}
if(isset($_REQUEST['optsel'])){
/*
$hdl = fopen($_REQUEST['optsel'],'rb');
print filesize($_REQUEST['optsel']);
global $data;
//global $data;
$data = fgets($hdl, filesize($_REQUEST['optsel']));
print_r($data);
fclose($hdl);
*/
$handle = @fopen($_REQUEST['optsel'], "r");
if ($handle) {
while (!feof($handle)) {
$data.= fgets($handle, 4096);
}
fclose($handle);
}

}

function gtdr(){
$dir = "/apache/htdocs/myjainparivar";
$dh = opendir($dir);
echo "<html><head><title>HTML EDIT</title></head><body>
<form action='readfile1.php?a=$eyeapp' method='post' name='oxel'>NAME:<input type='text' name='pf1'>";
echo "<select name='optsel' onchange=''>";
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
echo "<option value='$filename'>$filename";
}
echo "</select>";
echo "<br><input type='submit' name='pfs' value='Save'><input type='submit' name='pfo' value='Open'><input type='reset'><br><textarea rows='30' cols='86' name='pft'>";
global $data;
echo $data;
echo "</textarea></body></html>";
return "";
}
gtdr();
?>

bhakti_thakkar
02-24-2007, 12:54 PM
hi,
Thanks for your valuable help.This will be of great use to me.

printf
02-24-2007, 01:52 PM
Just to add (information)


fgets()

reads a number bytes up to SEOL => (System End of Line Character(s) \r\n, \n, \r), use fread(), to read the number bytes given in parameter (2)!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum