Help - Search - Members - Calendar
Full Version: Uploaded .txt Files With No Carriage Return
Zymic Webmaster Forums > Zymic Free Web Hosting > Zymic Free Web Hosting - General Discussion & Help
sandybox
Hello, whenever I upload a .txt file to my site for users to download, the downloaded version always has a line feed but no carriage return, so when opened in Notepad rather than each line being on a new line, they are seperated by a small square used to represent the line feed. Does anybody know what could be causing this?
DroNix
what are you using the upload this file, and program are you using to make this file?
Jacob
Due to .txt files being plain text files, no form of formatting is processed via the browser (line breaks, etc) . If you need to have formatting, I recommend HTML/XML for the feed. That way, you can use formatting and get your desired outcome.
sandybox
QUOTE(DroNix @ Mar 30 2010, 02:36 AM) *
what are you using the upload this file, and program are you using to make this file?

FlashFXP to upload it, Microsoft Notepad to create it.

QUOTE(NaRzY @ Mar 30 2010, 11:26 AM) *
Due to .txt files being plain text files, no form of formatting is processed via the browser (line breaks, etc) . If you need to have formatting, I recommend HTML/XML for the feed. That way, you can use formatting and get your desired outcome.


Thanks for the info but the text file isn't being processed by the browser like that, it just gets directly downloaded to the users system, then upon the user running it in Notepad, rather than the proper Windows carriage returns and line feeds, it just seems to have the UNIX line feeds.

The reason I want this is because I've written a few mIRC snippets/addons that i'd like people to be able to download, and seeing as my .mrc files were being rejected I had to rename them to .txt

sandybox
anybody? sad.gif
LooongSnake
QUOTE(sandybox @ Mar 30 2010, 12:53 PM) *
Microsoft Notepad to create it.

Use Pro Notepad like http://notepad-plus.sourceforge.net/uk/site.htm

Because Microsoft Notepad displays incorrectly
sandybox
QUOTE(LooongSnake @ Mar 31 2010, 12:39 PM) *
Use Pro Notepad like http://notepad-plus.sourceforge.net/uk/site.htm

Because Microsoft Notepad displays incorrectly


I already use Notepad++, viewing the files myself is not a problem. The problem is getting anyone who downloads the text file to also download extra software just to view it. Not very user-friendly, especially for a simple .txt file.

I think the only workaround is to dynamically make some sort of duplicate in PHP with the proper \r\n for Windows, then serve that to the user rather than the original. Seems like a lot of hassle just for a text file though sad.gif
Jacob
Have you tried changing your encoding to something more widely accepted by systems?
sandybox
QUOTE(NaRzY @ Apr 1 2010, 10:14 AM) *
Have you tried changing your encoding to something more widely accepted by systems?


Yea I did try uploading it in different encodings, but the server serves it as UNIX regardless, it's like there's some kind of conversion when uploaded because the file sizes become smaller, presumably because the carriage returns are stripped.

One thing that may be of note is when the files are downloaded via FlashFXP they are back in the proper encoding, but I think that's just because the software converts it back for me
sandybox
OK thanks for the ideas guys, I've come to a solution. See, when the .txt file is uploaded to the server, the server automatically strips every carriage return from the file.

So I wrote a small PHP script which takes a text file as input and creates a new text file with the proper carriage returns and line feeds for Windows systems.

If anybody else would like to use it, here:
CODE
<?php
    if (isset($_POST["old"]) && isset($_POST["new"])) {
        if ($_POST["old"] == "" || $_POST["new"] == "") {
            print "Error: Parameters Missing";
            exit;
        }
        $nfile = $_POST["new"]
        $ofile = $_POST["old"];
        $lines = file($ofile, FILE_IGNORE_NEW_LINES);
        if ($fhandle = @fopen($nfile, "w")) {
        foreach ($lines as $line_num => $line) {
            fwrite($fhandle, $line . "\r\n");
        }
        print "Successfully converted " . basename($ofile) . " to Windows encoding <br />";
        print "<a href=\"" . $nfile . "\">Download " . basename($nfile);
        }
        else {
        print "Failed to open file stream. Did you set permissions for writing?";
        exit;
        }    
    }

    else {
?>
    <h2>Unix to Windows .txt Converter</h2>
    <h5>Directory permissions must be set to allow writing by the group</h5>
    <form action="" method="post">
    <b>Original File:</b> <input type="text" name="old" />
    <p><b>New File:</b> <input type="text" name="new" /> </p>
    <input type="submit" value="Submit" />
    </form>
<?
    }
?>


Note: You must set writing permissions for the group on the directory you're working on.
Also, you should place the file you save this as in the directory you're working on
Novalia
I had the exact same problem and managed to get around it simply by changing the extension of the file (for example, "test.txt" became "test.data") before uploading it. After that, I restored the extension of the uploaded file, and the contents were left untouched. Aside from the issue concerning the carriage returns, this method also prevents other modifications from being made, such as the removal of blank lines at the beginning or the end of a document. Compressing the file is another viable option.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.