Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > using php to download a picture?

using php to download a picture?
Thread Tools
Fresh-Faced Recruit
Join Date: May 2005
Status: Offline
Reply With Quote
Oct 2, 2006, 09:16 PM
 
Hi,

Could someone please post some sample code for using PHP to download a jpeg file from a web site and save it to a local directory?

Thanks!
     
Senior User
Join Date: Jul 2004
Status: Offline
Reply With Quote
Oct 2, 2006, 09:56 PM
 
Well how do you want to access it? http? local network? ssh? ftp? there are many ways to access a file and store it contents somewere else. Some ways will be faster than others and more reliable (such as a local network).

But Copy function should cover most of that.
PHP: copy - Manual
Only problem with that is across a http you need to have fopen enabled.
You shouldn't make fun of nerds... you'll be working for one some day.
     
Fresh-Faced Recruit
Join Date: May 2005
Status: Offline
Reply With Quote
Oct 3, 2006, 09:35 PM
 
Thanks for the reply. Yes, I'd like to use HTTP to download and save a picture from website AAA to my local hard drive. How would this be done in php? Can it be done without issueing operating system calls (like when you use php to request a directory's contents, etc)

Thanks.
     
Senior User
Join Date: Jul 2004
Status: Offline
Reply With Quote
Oct 3, 2006, 10:00 PM
 
Do you got fopen allowed in your php config? In fact I think you need fopen to do this at all...

Copied from php.net/copy
Code:
<?php $file = 'http://somesite.com/image.gif'; //The Orginal File $newfile = 'images/image.gif'; //The New File. if (!copy($file, $newfile)) { echo "failed to copy $file...\n"; } ?>
Rememberr on the $newfile variable were it saves depends on how you set it up.
if you use a / you need the full path.
/home/me/public_html/images/image.gif

if you dont use a / you just need the relitive (?) directory of the image. So if saveimage.php is in /home/me/public_html/saveimage.php you just need
images/image.gif

But I always suggest using full path. Incase you move the file. If you move servers or path to images changes you will have to update though.
You shouldn't make fun of nerds... you'll be working for one some day.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Oct 3, 2006, 10:10 PM
 
The code provided will copy the image to a directory on the webserver... I think this guy is looking for a way to invoke the browser's save file function to allow users to save to their hard drive.

I don't think this is possible for security reasons in non IE browsers, but I could be wrong.
     
Senior User
Join Date: Jul 2004
Status: Offline
Reply With Quote
Oct 3, 2006, 10:56 PM
 
Well Then.. We Can Create the Image with php


Code:
<?php $file = 'http://forums.macnn.com/images/mainheader1.gif'; //Change to image... $filename = '/home/me/public_html/images/image.gif'; //Must be exact location. header("Content-type: image/gif"); //Change if you dont use gif $string = ''; $im = imagecreatefromgif($file); //Change if you dont use gif $orange = imagecolorallocate($im, 220, 210, 60); $px = (imagesx($im) - 7.5 * strlen($string)) / 2; imagestring($im, 3, $px, 9, $string, $orange); imagegif($im, $filename); //Change if you dont use gif //imagegif($im); //Uncomment to have it save and output & Change if you dont use gif imagedestroy($im); ?>
You shouldn't make fun of nerds... you'll be working for one some day.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Oct 3, 2006, 11:09 PM
 
I've only ever used ImageMagick for basic image resizing, will creating an image like that just save to a local directory, or will it prompt you to save the file?

I'm wondering if there is a way to disable Apache's awareness of MIME types in a particular directory, so that anything placed in this directory will prompt you to download the file. Of course, this approach would only work if you have access to edit your webserver config and restart the server.
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Oct 4, 2006, 03:18 PM
 
Originally Posted by besson3c
The code provided will copy the image to a directory on the webserver... I think this guy is looking for a way to invoke the browser's save file function to allow users to save to their hard drive.

I don't think this is possible for security reasons in non IE browsers, but I could be wrong.
You can't force the browser to do something from PHP. By the time the data gets to the browser, it's already processed (You already know this though). This is what Javascript is for.
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Fresh-Faced Recruit
Join Date: May 2005
Status: Offline
Reply With Quote
Oct 4, 2006, 03:23 PM
 
Just to help answer any questions, I'm trying to create a web site (A) that will "grab" a jpeg file from web site B and save it to the web site directory of web site A. So I guess it would be considered one web site copying a file from another web site.

Any suggestions and code is great appreciated.
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Oct 4, 2006, 03:27 PM
 
I tried to do a file copy with copy over http. I could not get it to work for the life of me. It would 9 times out of 10 mis-write the file as though it didn't know the filesize. I never found a solution and ended up going with FTP. ( I was working between two servers that we both controlled though)
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
Mac Elite
Join Date: Mar 2001
Location: CO
Status: Offline
Reply With Quote
Oct 8, 2006, 07:03 AM
 
One form of "grabbing" of the image is done when you http the site? Then the jpg will be in the browser's cache. But your *browser* is doing the grabbing.
Then you need to be using some software (not nec. PHP) on *your machine* (not the server).
Seems like it's a Mac-pgming issue, not a server-side PHP.

Perhaps you could develop a PHP app to do the dl to *server* ... then later dl from server to your local machine?

Google (obviously) manages to rip material out of web sites -- to store in their archives, etc. Is that type of operation you want to achieve?
TOMBSTONE: "He's trashed his last preferences"
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Oct 8, 2006, 09:51 AM
 
is the PHP "Curl" extension enabled?
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Oct 8, 2006, 09:56 AM
 
Try this:

Code:
$image = file_get_contents('http://image_url'); file_put_contents('/path/to/file/to/save', $image);
     
Fresh-Faced Recruit
Join Date: May 2005
Status: Offline
Reply With Quote
Oct 10, 2006, 09:19 PM
 
thanks for the suggestion. I haven't upgraded to php5 yet, so is there a php4 approach that you'd recommend? I'm trying to use fopen() and fclose(), but no luck yet.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Oct 10, 2006, 09:29 PM
 
Originally Posted by mondayisgreat
thanks for the suggestion. I haven't upgraded to php5 yet, so is there a php4 approach that you'd recommend? I'm trying to use fopen() and fclose(), but no luck yet.

The code I provided in my last post is php4 compatible. Does it work?
     
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Oct 10, 2006, 10:07 PM
 
Originally Posted by mondayisgreat
thanks for the suggestion. I haven't upgraded to php5 yet, so is there a php4 approach that you'd recommend? I'm trying to use fopen() and fclose(), but no luck yet.
Where are you having trouble?
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:37 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2