What's wrong with this code

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
alexvorn2
Forum Newbie
Posts: 4
Joined: Sat Feb 13, 2010 7:34 am

What's wrong with this code

Post by alexvorn2 »

please help to write binary data to a file:

I use this code but no work:

<?php
$fr = fopen('wtf.png', 'r');
$fr2 = fopen('wtf2.png', 'w');
$data = fread($fr, 299052);
fputs($fr2, $fr, 299052);
fclose($fr);
?>

Can you please tell me what's the problem with the code?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What's wrong with this code

Post by requinix »

Try adding the "b" flag to both fopens.
alexvorn2
Forum Newbie
Posts: 4
Joined: Sat Feb 13, 2010 7:34 am

Re: What's wrong with this code

Post by alexvorn2 »

I get this error:
Warning: fputs() expects parameter 2 to be string, resource given in C:\xampp\htdocs\test\index.php on line 5
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What's wrong with this code

Post by requinix »

alexvorn2
Forum Newbie
Posts: 4
Joined: Sat Feb 13, 2010 7:34 am

Re: What's wrong with this code

Post by alexvorn2 »

I visited that page a lot of times before, but don't see the solution..
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What's wrong with this code

Post by requinix »

Given that fputs is an alias of fwrite,
int fwrite ( resource $handle , string $string [, int $length ] )
The second argument is supposed to be a string. $fr is not a string. You're using the wrong variable.
Post Reply