Jump to content

loading text file with html formating into a textarea


Recommended Posts

Hello,

 

Im very new to php so im still trying to get to grips with the basics so any help on this subject would be greatly appreciated!

 

basically, Im creating a band music website and I have it so the news section loads dynamically from a text file which has html formatting.  However, I have created a section of the website for admin and basically the idea is that my news.txt file loads into a textarea(within a form), the user can edit the text file and add whatever they like to it, using html fomatting (as you would on myspace etc) and then save/write back to the same text file with the updated changes.  I have gotten all of this to work, except for one tiny thing:

 

when i post the information back to the text file, PHP puts in a back slash before and after any of the " " that i have for my HTML formatting. for example it will change:

 

<table width="590" height="245" border="0" align="left" cellpadding="0">        to:

<table width=\"590\" height=\"245\" border=\"0\" align=\"left\" cellpadding=\"0\">

 

and as a result it messes up the formatting of my news section.  So i would just like to know how to achieve all this without PHP adding in those back slashes?

 

any help would be greatly appreciated, hope ive explained that well enough.

Ross

 

 

this seems to be on the right track all right, but i dont think im using it properly.  Is it possible to write to the text file without the backslashes being added in the first place?  The only way I could get the stripslashes to work was on reading the text file back into the website....which works once.  But if you update the news section more than once via the admin section....it obviously adds double the amount of forward slashes.

 

here is the code for the write to file, as you can see I also tried putting the stripslashes just before it writes the content, but that hasnt worked.  sorry bout this.  if you'd like to see the other code i can post it up too?

 

<?php

# set up general write to check permissions

$filename = 'news.txt';

 

 

if (!$fp = fopen($filename, 'w+')) {

print "Cannot open file ($filename)";

exit;

}

 

$somecontent =  $story;

stripslashes($somecontent);

 

if (!fwrite($fp, $somecontent)) {

print "Cannot write to file ($filename)";

exit;

}

//remembering to close the file.

fclose($fp);

?>

 

 

what is $story? I dont see anywhere where its set. also wrap your code in code tags in the future, it makes it easier to read :)

 

echo $somecontent before you write it to the file and see what happens?

hello again,

 

here is the code to my admin form.  you can ignore the top part i guess, thats all fine.  its literally just when it saves back to news.txt that it adds in the backslashes.  Hope you can help, sorry if this is a mess:

<html>

<head>
    <title>edit news</title>
    
    <?php

if ($_SERVER['REQUEST_METHOD'] == "POST")
{
    /* SUBMITTED INFORMATION - use what you need
     * temporary filename (pointer): $imgfile
     * original filename           : $imgfile_name
     * size of uploaded file       : $imgfile_size
     * mime-type of uploaded file  : $imgfile_type
     */

     /*== upload directory where the file will be stored 
          relative to where script is run ==*/
    
    $uploaddir = ".";
    

    /*== get file extension (fn at bottom of script) ==*/
    /*== checks to see if image file, if not do not allow upload ==*/
    $pext = getFileExtension($imgfile_name);
    $pext = strtolower($pext);
    if (($pext != "jpg")  && ($pext != "jpeg"))
    {
        print "<h1>ERROR</h1>Image Extension Unknown.<br>";
        print "<p>Please upload only a JPEG image with the extension .jpg or .jpeg ONLY<br><br>";
        print "The file you uploaded had the following extension: $pext</p>\n";

        /*== delete uploaded file ==*/
        unlink($imgfile);
        exit();
    }


    //-- RE-SIZING UPLOADED IMAGE

    /*== only resize if the image is larger than 250 x 200 ==*/
    $imgsize = GetImageSize($imgfile);

    /*== check size  0=width, 1=height ==*/
    if (($imgsize[0] > 250) || ($imgsize[1] > 200)) 
    {
        /*== temp image file -- use "tempnam()" to generate the temp
             file name. This is done so if multiple people access the 
            script at once they won't ruin each other's temp file ==*/
        $tmpimg = tempnam("/tmp", "MKUP");

        /*== RESIZE PROCESS
             1. decompress jpeg image to pnm file (a raw image type) 
             2. scale pnm image
             3. compress pnm file to jpeg image
        ==*/
        
        /*== Step 1: djpeg decompresses jpeg to pnm ==*/
        system("djpeg $imgfile >$tmpimg");
        

        /*== Steps 2&3: scale image using pnmscale and then
             pipe into cjpeg to output jpeg file ==*/
        system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$imgfile");

        /*== remove temp image ==*/
        unlink($tmpimg);
    }


    /*== setup final file location and name ==*/
    /*== change spaces to underscores in filename  ==*/
    $final_filename = str_replace(" ", "_", $imgfile_name);
    $newfile = $uploaddir . "../images/$final_filename";
    
    /*== do extra security check to prevent malicious abuse==*/
    if (is_uploaded_file($imgfile))
    {

       /*== move file to proper directory ==*/
       if (!copy($imgfile,"$newfile")) 
       {
          /*== if an error occurs the file could not
               be written, read or possibly does not exist ==*/
          print "Error Uploading File.";
          exit();
       }
     }

    /*== delete the temporary uploaded file ==*/
    unlink($imgfile);
  
    /*== DO WHATEVER ELSE YOU WANT
         SUCH AS INSERT DATA INTO A DATABASE  ==*/
}
?>
    
    
</head>
<body>

<h2>Upload Content</h2>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?> " method="POST" enctype="multipart/form-data">
    <p>
      <input type="hidden" name="MAX_FILE_SIZE" value="50000">
    </p>
    <table width="777" border="0">
      <tr>
        <td width="207">Upload Image: </td>
        <td width="322"><input type="file" name="imgfile"></td>
        <td width="234" rowspan="3">
        
         <?php print("<img src=\"../images/$final_filename\">");

		 echo $final_filename;
		 $filenameis = $final_filename;
		 $path = "../images/" . $final_filename;
	 ?>
         
        </td>
      </tr>
      <tr>
        <td> </td>
        <td><div align="right"><font size="1">Click browse to upload a local file
          <input type="submit" value="Upload Image">
        </font></div></td>
      </tr>
      <tr>
        <td height="22"> </td>
        <td> </td>
      </tr>
    </table>
</form>


    <form id="form1" name="form1" method="post" action="php_writer.php">
      <table width="784" border="0" id="cont">
        <tr>
        
          <td width="59"><p>Image path:</p></td>
          <td width="715"><label>
            <input name="filenamer" type="text" id="filenamer" value= " <?php echo $path;?>" size="47" />
          </label></td>
        
        </tr>
        
        <tr>
        	<td>Edit/Add & Submit </td>
          
            	<td> <textarea name="story" cols="110" rows="25" id="story"> 
			<?php  $news = file_get_contents ('news.txt');
			print($news);?>
                
			  </textarea></td>
        </tr>
        <tr>
          <td colspan="2">
          
          <div align="right">
            <input type="submit" name="submit" id="submit" value="Submit" />
          </div></td>
          
        </tr>
      </table>
      </form>
         
</body>
</html>

 

and php_writer looks like this:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<?php 


# set up general write to check permissions 
$filename = 'news.txt'; 


if (!$fp = fopen($filename, 'w+')) { 
print "Cannot open file ($filename)"; 
exit; 
} 

/*$somecontent = $story;*/
$somecontent =  $story;
stripslashes($somecontent);

if (!fwrite($fp, $somecontent)) { 
print "Cannot write to file ($filename)"; 
exit; 
} 
//remembering to close the file. 
fclose($fp); 
?> 
<body>

<a href="news.txt">news.txt</a>
</body>
</html>

 

 

My fault,

 

stripslashes was right, i was just using it in the wrong places.  I needed to have it while loading it into my textarea in the admin section and also have it on my main news page.  I only had it just before it writes to the text file. This works fine now.  would be nice to know if there is a way to make it so it doesnt write the backslashes to file.  But this is solved for now.

 

thanks 4 the help :) :) :)

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.