Cannot copy image file using fread and fwrite

anchitjindal07's Avatar, Join Date: Jul 2009
Go4Expert Member
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...
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Make the mode of read and write as binary. Also if possible use the copy function. http://php.net/manual/en/function.copy.php
0
anchitjindal07's Avatar, Join Date: Jul 2009
Go4Expert Member
@Shabbir
I have already opened the files in binary mode. See the first two if statements
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I missed those 2 lines but I guess the way you are trying probably is messing the data completely.
0
anchitjindal07's Avatar, Join Date: Jul 2009
Go4Expert Member
@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??
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
pein87's Avatar
Ambitious contributor
Hey try this I didn't test it since I don't have files named like those but this one should work.

PHP Code:
<?php
$imgs 
= array('image.jpg','hi.jpg','image2.jpg','hi2.jpg');

if (
$rh fopen($imgs[0], 'rb') == false)
{
echo 
"Cannot open " $imgs[0];    
}
else
{
    echo 
"image " $imgs[0] . " has been read.";
}
 
if (
$wh fopen($imgs[1], 'wb') == false)
{
    echo 
"Cannot open " $imgs[1];
}
else
{
    echo 
"image " $imgs[1] . " has been read.";
}

$i;
$imgCount count($imgs);

 for(
$i == 2$i $imgCount$i++)
 {
     
touch($imgs[$i]);//make new blank images for read and write;
 
}
 
 if(!
file_exists($imgs[2]) && !file_exists($imgs[3])) // check if files were created
 
{
     echo 
"Blank Copies were not made of the originals";
 }
 else 
// blanks were made lets get to work on writing the data to them
 
{
    
    
$copiedImgDat fopen($imgs[2], 'rb');
    
$copiedImgDat2 fopen($imgs[3], 'wb');
    
    
$writeCopyDat fwrite($copiedImgDat,$rh);
    
$writeCopyDat2 fwrite($copiedImgDat2,$wh);
        
 }

if (
$writeCopyDat == fale)
{
echo 
"unable to write to " $imgs[2]; 
}
else
{
    echo 
$imgs[0] . " has been copied and is named " $imgs[2];
}
if(
$writeCopyDat2 == fale)
{
    echo 
"unable to write to " $imgs[3];
}
else
{
    echo 
$imgs[1] . " has been copied and is named " $imgs[3];
}

fclose($rh);
fclose($wh);
fclose($copiedImgDat);
fclose($copiedImgDat2);

$j;

// echo them out to see if they copied properly
for($j == 0$j $imgCount$j++)
{
echo 
"<img src=\"$mgs[$j]\" title=\"$imgs[$j]\" />";
}
?>

Last edited by pein87; 8May2011 at 09:29..