TurboTax Software - Credit Cards - Payday Loan - Record Internet Radio with Tags - Myspace Backgrounds

PDA

View Full Version : I Need To Download Image From Web To Server Using Php


deriklogov
Mar 4th 2007, 8:56 am
Hi,

Basicly what I need says in the title: I NEED TO DOWNLOAD IMAGE FROM WEB TO SERVER USING PHP, is that possible ? if yes, does anyone has simple example of the code which can do that ?

Icheb
Mar 4th 2007, 9:03 am
SEARCH PHP.NET FOR FOPEN AND FGET. You will find sample code there.

adsblog
Mar 4th 2007, 9:10 am
use copy() (http://ir.php.net/manual/en/function.copy.php)

deriklogov
Mar 4th 2007, 10:15 am
I check it out those , but I cant find any samples over there that would download image and save it to disk, just some examples of getting .html documents

Jim_
Mar 4th 2007, 2:06 pm
Something like this should work.
<?php
$filelocation = "http://www.site.com/folder/image.gif";
$foldertosave = "./images/";

$open = fopen($filelocation, "rb");
$contents = stream_get_contents($open);
fclose($open);
$save = fopen(array_pop(split("/", $filelocation)), "a");
fwrite($save,$contents);
fclose($save);
?>
You'll probably want to do some verification to make sure that the file being uploaded is in fact an image. The best way I can think of doing that is to open the image using the GD library and if it returns an error, the file is not an image.

deriklogov
Mar 4th 2007, 2:19 pm
Thank Jim, Its working

Icheb
Mar 4th 2007, 7:37 pm
I check it out those , but I cant find any samples over there that would download image and save it to disk, just some examples of getting .html documents
So? It's all a file, nothing else.

Chemo
Mar 5th 2007, 8:19 am
I would recommend cURL using the CURLOPT_INFILE option.

ccharp
Mar 6th 2007, 2:32 am
nice one guys, i've been looking for something like this as well...thanks

jitesh
Mar 14th 2007, 3:40 am
<?php

$filename=$_REQUEST['filename'];
$filename = realpath($filename);

$file_extension = strtolower(substr(strrchr($filename,"."),1));

/****Set Header Content Type based on File Extension****/
switch ($file_extension) {
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpe":
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
if (!file_exists($filename)) {
die("NO FILE HERE");
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header('Content-Description: File Transfer');
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
set_time_limit(0);
@readfile("$filename") or die("File not found.");

?>

deriklogov
Mar 14th 2007, 5:02 am
Thank You for Your Post, I already found one and its working fine.