Re: Different size of file on server and on output

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



M. Sokolewicz wrote:
> Pavel Janda wrote:
>> Hello to everybody,
>>
>> I have this problem with downloading files via PHP. For illustration -
>> I am using this fragment of code:
>>
>> <?php
>>
>>     header("Content-type: $contenttype; name=\"$outputfilename\"");
>>     header("Content-disposition: form-data;
>> filename=\"$outputfilename\"");
>>
>>     $fp=fopen($filename,'r');
>>     while (!FEof($fp)):
>>         $filedata.=fread($fp,8192);
>>     endwhile;
>>     fclose($fp);
>>     echo $filedata;
>>
>> ?>
>>
>> The problem is, that the file on server has 16857 bytes and saved file
>> has 16858. In another case downloaded file has everytime 1 byte more
>> than the original file on server.
>>
>> When I put JPEG or ZIP forexample to output, there is no problem with
>> opening file like this. Bud when I am trying .docx (Office2007 file
>> format) it occures mistake during opening this file in MSOffice...
>>
>> Can anybody helps me with this problem? Why is the file size
>> increasing one byte? Whitch byte it is?
>>
>> Thanks a lot!
>> Pavel
> 
> Probably that whitespace after your last ?>
> What I would recommend is to just leave off the last ?>, it's not
> required and by not adding it you won't see those annoying problems with
> whitespaces causing corrupt data.

I agree.

Also a much simpler way of outputting a file that doesn't use up memory,
is just to replace:

     $fp=fopen($filename,'r');
     while (!FEof($fp)):
         $filedata.=fread($fp,8192);
     endwhile;
     fclose($fp);
     echo $filedata;


With:
     readfile($filename);


http://uk.php.net/manual/en/function.readfile.php

Col.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux