Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   Cannot copy image file using fread and fwrite (http://www.go4expert.com/forums/copy-image-file-using-fread-fwrite-t25687/)

anchitjindal07 3May2011 12:25

Cannot copy image file using fread and fwrite
 
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...

shabbir 3May2011 12:37

Re: Cannot copy image file using fread and fwrite
 
Make the mode of read and write as binary. Also if possible use the copy function. http://php.net/manual/en/function.copy.php

anchitjindal07 3May2011 12:44

Re: Cannot copy image file using fread and fwrite
 
@Shabbir
I have already opened the files in binary mode. See the first two if statements

shabbir 3May2011 15:34

Re: Cannot copy image file using fread and fwrite
 
I missed those 2 lines but I guess the way you are trying probably is messing the data completely.

anchitjindal07 3May2011 16:29

Re: Cannot copy image file using fread and fwrite
 
@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??

shabbir 3May2011 18:57

Re: Cannot copy image file using fread and fwrite
 
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.

pein87 8May2011 09:22

Re: Cannot copy image file using fread and fwrite
 
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]\" />";
}
?>



All times are GMT +5.5. The time now is 17:11.