Jump to content

[SOLVED] fwrite remote image not working


scarhand

Recommended Posts

im trying to get an image from a remote URL and write it to a local folder

 

now it gets the image, but the image is always corrupt....

 

code:

 

$sql = mysql_query("select * from prods order by id asc limit 1");

while ($row = mysql_fetch_assoc($sql))
{
  $imgo = $row['img'];
  $imgo = str_replace('product/', '', $imgo);
  $imgu = "http://www.imgsite.com/$img";
  
  $content = file_get_contents($imgu);
  $dir = dirname($_SERVER['SCRIPT_FILENAME']) . '/img';
  $fp = fopen($dir . '/' . $imgo, 'w');
  
  fwrite($fp, $content);
  fclose($fp);
}

Link to comment
Share on other sites

How do you know it is corrupt? How are you attempting to open or display it? Is the resulting file the expected size? Smaller? Larger? Have you opened the file using a programming editor to examine what it actually contains?

 

The GD functions should not be used just to "copy" an image. They take a huge amount of resources and processing time because they operate on an uncompressed bitmap version of the image. You also need to know or find the image type before you can use the correct functions.

Link to comment
Share on other sites

How do you know it is corrupt? How are you attempting to open or display it? Is the resulting file the expected size? Smaller? Larger? Have you opened the file using a programming editor to examine what it actually contains?

 

The GD functions should not be used just to "copy" an image. They take a huge amount of resources and processing time because they operate on an uncompressed bitmap version of the image. You also need to know or find the image type before you can use the correct functions.

 

I opened the file in internte explorer and its a red X. I opened it in windows photo gallery and it tells me "the image is damaged or corrupted".

 

But the image is the same size as the one I used fwrite on.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.