Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php text download line length 1

Status
Not open for further replies.

anorakgirl

Programmer
Jun 5, 2001
103
GB
Hi,

I have some code which takes a query and outputs the results in tab delimited format, which i require for an import into a different program. However, one of the fields is quite a bit description field (up to 4000 chars). When i do the download, some of the really long lines are wrapped onto a second line, which messes up the import. I'm not sure if it is php or apache which would automatically wrap lines at a certain number of characters, and if there is any thing I can do about it? It is not my code which is wrapping lines, and I have removed all line breaks from the field data.

Thanks!
Tamsin

Code:
function create_csv_file($info, $sql, $db)
{
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$info.txt");
    $result = pg_query($db, $sql);
    $num = pg_num_rows($result);
    
    //output the header row
    $r = pg_fetch_row($result, 0);
    for ($j=0; $j < count($r); $j++) 
    {
        $f = pg_field_name($result, $j);        
        echo "$f\t";
    }
    echo "\r\n";

    //output the first row
    for ($j=0; $j < count($r); $j++) 
    {
        echo "".output_field($r[$j])."\t";
    }

    echo "\r\n";
    //output the rest of the rows
    for($i=1; $i < $num; $i++) 
    {
        $r = pg_fetch_row($result, $i);
        for ($j=0; $j < count($r); $j++) 
        {
            echo "".output_field($r[$j])."\t";
        }
        echo "\r\n";
    }
}




function output_field($value) {
        $strret = str_replace(array("\n","\r","\r\n","\t"),array(" "," "," "," "),strip_tags($value));
        return $strret;
}

~ ~
 
to my knowledge, neither php nor apache will "wrap lines".

btw this line
Code:
header("Content-Disposition: attachment; filename=$info.txt");
might be better set as
Code:
header("Content-Disposition: attachment; filename=\"$info.txt\"");
as the filename should ideally be in double quotes.

to debug, try changing this loop to use a different line differentiator and then examine the source code of the output for what causes the line break (bearing in mind that it might be a limitation in excel or your text reader)
Code:
        for ($j=0; $j < count($r); $j++)
        {
            echo "".output_field($r[$j])."\t";
        }
        echo "##LINEBREAKSHOULDBEHERE##";
 
Hi,

Thanks for the tips - It looks like it was notepad that was wrapping the lines. I'd thought it occured with Excel too, but realised I'd been opening it directly to notepad, then saving it somewhere, so notepad had already scuppered it before it got to Excel.

I'm using a different text editor to look at it now and it seems fine. Thanks!

Tamsin

~ ~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top