Help, Problem with fputs() !!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bugworm
Forum Newbie
Posts: 8
Joined: Fri Nov 29, 2002 7:54 pm

Help, Problem with fputs() !!!

Post by Bugworm »

hi guys, this problem is realy ... stupid.. i dont know whats wrong....

$fp=fopen($filename, "wb");
flock($fp,1);
fputs($fp, $variable, strlen($variable));
flock($fp,3);
fclose($fp);


the script should write the $variable in the file with the $filename...
o.k thats simple.. and it works wonderfull... BUT now comes the problem...

my $variable is now 7400 chars long...

and all what it now does, is just emptiingt the file, just cleaning out all data... without putting anything in...

its realy.. crazy...
i am running that script on a win32 machine with apache an php4 ...

i am realy burn out, after trying 3 hours to get this $... working ;-)

thanks for all help

Bugworm 8O
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if it really is the length of the variable you might chop it into smaller pieces
http://www.php.net/manual/en/function.chunk-split.php
e.g.

Code: Select all

$variable = chunk_split($variable, 2048, '');
foreach($variable as $piece)
	fputs($fp, $piece, strlen($piece));
Bugworm
Forum Newbie
Posts: 8
Joined: Fri Nov 29, 2002 7:54 pm

i tried that...

Post by Bugworm »

.... but first, well, chunk_split will not make an array, that is needed for a "foreach" function.. that dousnt work...

as i understand, chunk_split, just puts some end things after 2045 chars, and then putting the next together...

well, thanks for all, i tried your solution but it still doesn't work...

I tried the same thing on my unix-webserver, and there it works... hell, whats wrong with my machine *G* (o.k windows is on it...)

i tried to chop the $variable in little pieces, and fput every single peace.. but that doesnt work eather... he puts the LAST 3455 chars in the file, the other ones are somewhere in the endless space... but not in the file, were they should be.... :evil:

plz help... i am running crazy... 8O
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

uh, chunk_split was stupid.
But I tested fputs with the content of a whole mp3 (3mb) on my w2k box. No problem, files are identical ;)

Code: Select all

<?php
$fd = fopen('test.mp3', 'rb') or die('stupid.');
$content = '';
while(!feof($fd))
	$content .= fread($fd, 8192);
fclose($fd);
$fd = fopen('test.out.mp3', 'wb') or die('stupid.');
fputs($fd, $content, strlen($content));
?>
Bugworm
Forum Newbie
Posts: 8
Joined: Fri Nov 29, 2002 7:54 pm

Solution found !!!

Post by Bugworm »

Thanks for your help... the solution is found !!! *yipee*
well, i tested exactly the script you wrote here, and it worked... well... then i looked, what is different between your script and mine...
and it was the flock !!!!
i always used flock($fp, 1) bevore writing, and flock($fp, 3) after it, i read that in a book, and it always worked...

well now i looked what flock realy means, and how it works, and on php.net it says that 1 is for shared reading, and 2 for exclusive writing... 3 is the release
the whole thing is there to secure, that only one script can write into a file...

so, now i use flock($fp, 2) and it works wonderfull, ...
the only question that is left, why does it alows to write short things into files with flock($fp, 1)... that originally says that only READING is allowed... ;-)

maybe that is a major SECURITY problem ;-)
dont know...

main thing, it works now ...

thanks for your help !!!
Post Reply