Forum Moderators: coopster

Message Too Old, No Replies

Download a picture by URL. What's the best method?

         

iProgram

3:15 pm on Feb 18, 2006 (gmt 0)

10+ Year Member



I want to enable visitor to upload picture to my server, or simply provide an URL of that picture. Upload is easy and how to program with "Download by URL"? For example, a visitor provides "http://www.webmasterworld.com/ax/dlogo.png", how can I download this file to my server via PHP and handle all errors?

coopster

4:29 pm on Feb 18, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You should be able to just retrieve the image like any other url source ...
// I made up a POSTed form variable where a user puts in a url 
// You will want to edit this too, strip_tags(), etc. -- make
// sure it is a valid url before you try to retrieve contents!
// Should return something like ...
// http://example.com/images/image1.jpg
$imageFileName = basename [php.net]($_POST['imageFileName']);
// Gives us:
// image1.jpg
// Now run your edits against this to insure a valid filename
// and make sure you approve of the filename, extension, etc.
// Next you want to retrieve the binary file stream
$imageFile = file_get_contents [php.net]($_POST['imageFileName']);
// run more file edits here such as mime-type

If all is well, you can store the file, etc, etc.