Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

downloading an image from web site to web site?

Status
Not open for further replies.

ralph4208w

Programmer
Aug 29, 2006
13
US
Hi,

What's a good way to use php to take the an image from one web site ( ) and save it to my other web server ( Basically, I'd like to essential "scrape" an image to my web server...not that I'm going to be scraping anyone's website, but I want one of my websites to copy from another web site. (trust me, it's for an ethically good thing)






sites I like: .. let's me find new movies to watch :)
 
if you know the address of the resources you use any of the file functions (e.g. fopen + fread or file_get_contents)
 
This will do what you want:

Code:
<?php

$image = '[URL unfurl="true"]http://www.site.com/image.jpg';[/URL]

$read = fopen($image, "r");

stream_set_timeout($read, 180);
while ($line=fgets($read))
	{
	$read_content .= $line;
	}

fclose($read);

$full_file = '/full/path/to/your/server/account/image.jpg';

$handle = fopen($full_file, 'wb');
fwrite($handle, $read_content);
fclose($handle);

echo 'Done';

?>

NATE


Got a question? Search G O O G L E first.
 
Spyderix: does the stream not need to be binary safe for an image?
 
It's already in binary. Here's a quote from php.net

php.net said:
Note: As of PHP 4.3.2, the default mode is set to binary for all platforms that distinguish between binary and text mode. If you are having problems with your scripts after upgrading, try using the 't' flag as a workaround until you have made your script more portable as mentioned below.

Url:
NATE


Got a question? Search G O O G L E first.
 
Hi Spyderix.

I'm aware of that note but i have always been concerned by the later notes that read:
The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the 'b' in all other cases.

If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.

Note: For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen().

Note: Again, for portability, it is also strongly recommended that you re-write code that uses or relies upon the 't' mode so that it uses the correct line endings and 'b' mode instead.

as a result of these I now always use the "b" flag or, more often for complete file reads, use file_get_contents which is inherently binary safe.
 
that's what i'd do (although i'd actually use file_get_contents()).

i must admit that i've never tried not adding this so it could
well be that you're advice above was spot-on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top