download image script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

download image script

Post by jabbaonthedais »

I'm trying to make just a few lined script where users put in a url of an image, and have it download it to their computer. I don't know if you can do this because of permissions. The purpose of this is to get around the IE bug that makes you download jpegs as a bmp.

I tried using variations of the below code, but I guess the copy command won't do it.

Code: Select all

<?php
$file = http://comedyhaven.com/CHLogo.jpg
$name = test1
copy($file, $name.'.bak');
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that bug, is because the site doesn't send a content length header, from what I've seen.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

There are several reasons this bug happens. But for many IE users, they cannot download any images (gif or jpeg) without saving them as a .bmp. Mozilla and other browsers do not have this problem.

Thats why I want to make a simple script, for me and my friends to use.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

use [php_man]file_get_contents[/php_man] to download the file, [php_man]fopen[/php_man] and it's siblings to create the file on the server.
jabbaonthedais
Forum Contributor
Posts: 127
Joined: Wed Aug 18, 2004 12:08 pm

Post by jabbaonthedais »

I had to substitute file_get_contents for file because my php version is older.

Code: Select all

<?php
$url = 'http://comedyhaven.com/CHLogo.jpg';
list($protocol, $uri) = split('//', $url);
$html = file($url);
fopen("file.jpg", "wb");
?>
When I use this code, it creates the file on my server, but it's 0 bytes. It's not downloading it properlly.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code doesn't write anything to the file.
Post Reply