Cannot copy image file using fread and fwrite

Discussion in 'PHP' started by anchitjindal07, May 3, 2011.

  1. anchitjindal07

    anchitjindal07 New Member

    Joined:
    Jul 13, 2009
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    1
    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...
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. anchitjindal07

    anchitjindal07 New Member

    Joined:
    Jul 13, 2009
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    1
    @Shabbir
    I have already opened the files in binary mode. See the first two if statements
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I missed those 2 lines but I guess the way you are trying probably is messing the data completely.
     
  5. anchitjindal07

    anchitjindal07 New Member

    Joined:
    Jul 13, 2009
    Messages:
    22
    Likes Received:
    0
    Trophy Points:
    1
    @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??
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  7. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    Hey try this I didn't test it since I don't have files named like those but this one should work.

    PHP:
    <?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: May 8, 2011

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice