Jump to content

Unwanted entry's in text file.


jimj1165

Recommended Posts

Hi Everyone,

 

I am writing a PHP script to allow some of my site members to add links to their site. The script is setup to use a supplied link to an image, then make that image a clickable link which in turns opens that webpage.  Here is my code:

 

<?php
if($_POST['formsubmit'] == "Submit")
{
  $varLink = $_POST['Link'];
  $varImage = $_POST['Image'];
  $varDescription = $_POST['Description'];
}
$filea = "temp.tmp";
$fileb = "original.txt";
$filec = "newfile.txt";
$fa=fopen($filea,'w');                /* Open Temp File and Write $_POST data */
if($fa) {
   fwrite($fa,"<a href='$varLink'><img src='$varImage'></a><br>$varDescription<br><br>\n");
fclose($fa);
}
$fa=fopen($filea,'r');               /* Open Temp File and Read Contents */
if($fa) {
   $bufa=fgets($fa);
fclose($fa);
}
$fc=fopen($filec,'w');               /* Open New File to merge new and old Links */
if($fc) {
   fwrite($fc, $bufa);               /* Write Newest Link First */
}

$fb=fopen($fileb,'r');               /* Open Original file and Read it */
if($fb) {
   while(!feof($fb)) {               /* Loop through complete old file and write to new file */
     $bufb=fgets($fb);
     fwrite($fc, $bufb);
   }
}

fclose($fb);
fclose($fc);

unlink('original.txt');                  /* Delete Original File */
unlink('temp.tmp');                      /* Delete Temp File */
rename('newfile.txt', 'original.txt');   /* Rename New File to the Old Filename */

?>

 

NOTE The above code has been 'expanded' in my troubleshooting efforts... (i.e. I wrote the first write to file code, executed it and checked the file contents manually, then the 2nd step, etc....) so my final code will be shorter.

 

The problem I am having,.... when i write the 1st file 'temp.tmp', I get the expected data in the file (below).

 

<a href='http://www.somesite.com'><img src='http://www.somesite.com/pic.jpg'></a><br>Description of site<br><br>

 

However, when I read the temp.tmp and original.txt files and write it to the newfile.txt (to merge it all together) the expected data is there but with an extra entry after each 'good' entry (below):

 

<a href='http://www.somesite.com'><img src='http://www.somesite.com/pic.jpg'></a><br>Description of site<br><br>

<a href=''><img src=''></a><br><br><br>

 

I don't understand why i am getting the extra entry '<a href=''><img src=''></a><br><br><br>' in the file. Can anyone shed some light on this?

 

My head is sore from banging it on the wall!

Link to comment
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.