Retaining formatting in uploaded files

I'm having a little trouble with formatting in uploaded files. Basically an administrator can upload article files in a *.txt format. I then store the path of the textfile in a database so that I can access and display it as follows:
[code]
$fop = fopen($pathname); // open the file stream
$content = strip_tags(file_get_contents($pathname)); // get the contents of the uploaded article

if(!$content) {
echo 'There was an error opening the file';
}
fclose($fop); // close the file stream
echo "$content"; // display the article
}

// $pathname is just a pathname to the file
[/code]

The trouble is, when I display the file, I have no formatting at all. I realise *.txt doesn't have proper formatting but surely I should see some line breaks between the paragraphs?

How else could I do this? I don't want to allow the user to upload HTML files as I use the strip_tags() function to remove any tags in the file (else it could be a security risk as others could execute malicious PHP code or such like).

I've tried using RTF files but then I can see the formatting which the word processor puts in and it looks very ugly.

Please help,

Many thanks.

ITA

"Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Comments

  • When you open a text file it always has endlines at the end of a line.
    Maybe you could try to put this code in it:
    [code]
    $message = str_replace ("
    ", "
    ", "$message");
    [/code]

    PHP than replaces the enlines with br tags and if you echo that in your page you should get proper formatting.

    Good luck,

    --=][tReShR][=--

  • Thanks a million - that was urgent.

    ITA
    "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion