PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Different size of file on server and on output
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Different size of file on server and on output

Réponse
 
LinkBack Outils de la discussion
Vieux 22/10/2007, 12h57   #1
Pavel Janda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Different size of file on server and on output

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 s me with this problem? Why is the file size increasing
one byte? Whitch byte it is?

Thanks a lot!
Pavel
  Réponse avec citation
Vieux 22/10/2007, 13h26   #2
M. Sokolewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Different size of file on server and on output

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 s 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.

- Tul
  Réponse avec citation
Vieux 22/10/2007, 13h43   #3
Pavel Janda
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Different size of file on server and on output

Thanks a lot. However there wasn't any visible whitespace in code,
leaving of ?> ed... I haven't known, that it is not required ).

Thank you very much again!
Pavel

M. Sokolewicz napsal(a):
> 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 s 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.
>
> - Tul

  Réponse avec citation
Vieux 22/10/2007, 13h45   #4
Colin Guthrie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Different size of file on server and on output

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 s 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h34.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12216 seconds with 12 queries