Please tell what is wrong with following code:
<?php
if (($rh = fopen('image.jpg', 'rb')) === FALSE)
echo "Cannot open image.jpg";
if (($wh = fopen('hi.jpg', 'wb')) === FALSE)
echo "Cannot open hi.jpg";
if (fwrite($wh,fread($rh, filesize('image.jpg'))) == FALSE)
{
echo "unable to write to file";
exit;
}
fclose($rh);
fclose($wh);
?>
When I run the above code ,it creates a file hi.jpg whose size is same as image.jpg but when I try to open hi.jpg, it gives the following error:
Error interpreting JPEG image file (Not a JPEG file: starts with 0x47 0x49)
What can be the problem?? Please don't say to use copy() as I cannot do that. I have to copy image file using fread() and fwrite() only.
Thanks a lot...
|
Go4Expert Founder
|
![]() |
| 3May2011,12:37 | #2 |
|
Make the mode of read and write as binary. Also if possible use the copy function. http://php.net/manual/en/function.copy.php
|
|
Go4Expert Member
|
|
| 3May2011,12:44 | #3 |
|
@Shabbir
I have already opened the files in binary mode. See the first two if statements |
|
Go4Expert Founder
|
![]() |
| 3May2011,15:34 | #4 |
|
I missed those 2 lines but I guess the way you are trying probably is messing the data completely.
|
|
Go4Expert Member
|
|
| 3May2011,16:29 | #5 |
|
@Shabbir
Surprisingly the same code with same image is working on my friend's system having same operating system.. How it is possible?? What can be the possible cause?? |
|
Go4Expert Founder
|
![]() |
| 3May2011,18:57 | #6 |
|
Cannot comment on why it is working or why it is not working but what I can say is it is not the right way to make it work.
|
|
Ambitious contributor
|
|
| 8May2011,09:22 | #7 |
|
Hey try this I didn't test it since I don't have files named like those but this one should work.
PHP Code:
Last edited by pein87; 8May2011 at 09:29.. |

